简体   繁体   中英

Process client side array in KRL raised to Kynetx app

Overview

I am working on building a Kynetx ruleset that will find a bunch of Facebook ids that are on the page and then use the Kynetx Facebook module to get the Facebook avatar associated with that Facebook id. I have the JS that creates an array of Facebook ids on the page and I can process an array in KRL to retrieve Facebook avatars. What I don't have is how to get an array from the client side to the server side in KRL.

How can I get the array from the client side to the server side of KRL?

You can take a JavaScript array and convert it into a string and it will work if you decode it on the server side of KRL.

Example app code => https://gist.github.com/722536

Example app bookmarklet => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/send-array-to-kns-dev-bookmarklet.html

ruleset a60x442 {
  meta {
    name "array-passing-test"
    description <<
      array-passing-test
    >>
    author "Mike Grace"
    logging on
  }

  rule start_your_engines {
    select when pageview ".*"
    {
      notify("Running","...sending array to KNS") with sticky = true;
      emit <|
        app = KOBJ.get_application("a60x442");
        var numbers = [1,2,3,4,5];
        nums = JSON.stringify(numbers);
        app.raise_event("process_array", {"numbers":nums});
        $K("div.KOBJ_message").append("<br/>"+nums);
      |>;
    }
  }

  rule process_array {
    select when web process_array
    foreach event:param("numbers").decode() setting (number)
    {
      notify("number",number) with sticky = true;
    }
  }
}

Results of running app from bookmarklet on http://example.com/ 运行应用程序的结果

Answer

Unfortunately, the KRL JS runtime doesn't yet support sending arrays to the server side. There is a way to accomplish what you are wanting to do though.

Example

I built an example app that runs on this page with a bookmarklet that gets the tags that the question is tagged with and sends them to the server to be processed and then they come back.

Example app code => https://gist.github.com/707561

Example app bookmarklet => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/client-side-array-to-server-bookmarklet.html

Step by step explination of code example

  1. collect text in JS array
  2. convert array into csv string and append comma to make regex splitting easier
  3. raise event to KNS with csv string
  4. process rule pulls first value off
  5. rest of the values are saved to a new variable
  6. first value goes into a notify
  7. postlude sends remaining values to itself
  8. loops until done and returns directives back to the browser

Results of running app from bookmarklet:

从bookmarklet运行应用程序的结果

You can also do arrays of hashes if you JSON.stringify the array of hashes.

Example app:

ruleset a60x449 {
  meta {
    name "pass-hash-in-web-event-test"
    description <<
      pass-hash-in-web-event-test
    >>
    author "Mike Grace"
    logging on
  }

  rule start_this_party {
    select when pageview ".*"
    {
      notify("Now running","Building arrays to send to KNS") with sticky = true;
      emit <|
        var data = {};
        data.userData = JSON.stringify(
          [
            {"name":"MikeGrace","id":234232344},
            {"name":"TelegramSam","id":234089790234},
            {"name":"Alex","id":2300234234234}
          ]
        );
        app = KOBJ.get_application("a60x449");
        app.raise_event("process_me_data", data);
      |>;
    }
  }

  rule process_arrays_of_data {
    select when web process_me_data
    foreach event:param("userData").decode() setting (user)
    pre {
      userName = user.pick("$.name");
      userId = user.pick("$.id");
      output =<<
        <p>
          userName: #{userName}<br/>
          userId: #{userId}<br/>
        </p>
      >>;
    }
    {
      append("body", output);
    }
  }
}

Results of running on example.com

替代文字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM