KRL 中的处理客户端数组提升至 Kynetx 应用程序

发布于 2024-10-03 15:22:11 字数 232 浏览 11 评论 0原文

概述

我正在构建一个 Kynetx 规则集,它将查找页面上的一堆 Facebook id,然后使用 Kynetx Facebook 模块获取与该 Facebook id 关联的 Facebook 头像。我有在页面上创建 Facebook id 数组的 JS,我可以在 KRL 中处理数组以检索 Facebook 头像。我不知道如何在 KRL 中将数组从客户端获取到服务器端。

如何从 KRL 客户端获取数组到服务器端?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃不饱 2024-10-10 15:22:11

您可以获取 JavaScript 数组并将其转换为字符串,如果您解码它,它就会工作在 KRL 的服务器端。

示例应用程序代码=> https://gist.github.com/722536

示例应用书签 => 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;
    }
  }
}

http://example.com/
运行应用程序的结果

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/
results of running app

々眼睛长脚气 2024-10-10 15:22:11

回答

不幸的是,KRL JS 运行时尚不支持将数组发送到服务器端。有一种方法可以完成您想做的事情。

示例

我构建了一个示例应用程序,该应用程序在此页面上运行,并带有一个书签,该书签获取问题所标记的标签,并将它们发送到服务器进行处理,然后返回。

示例应用代码 => https://gist.github.com/707561

示例应用书签 = > http://mikegrace. s3.amazonaws.com/forums/stack-overflow/client-side-array-to-server-bookmarklet.html

代码示例的逐步说明

  1. 收集 JS 数组中的文本
  2. 将数组转换为 csv 字符串并将逗号附加到使正则表达式拆分更容易
  3. 使用 csv 字符串将事件引发到 KNS
  4. 处理规则将第一个值
  5. 从其余值保存到新变量
  6. 第一个值进入通知
  7. postlude 将剩余值发送到自身
  8. 循环直到完成并将指令返回到浏览器

结果从书签运行应用程序的结果:

从书签运行应用程序的结果

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:

results of running app from bookmarklet

懷念過去 2024-10-10 15:22:11

如果您对哈希数组进行 JSON.stringify,则还可以执行哈希数组。

示例应用:

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);
    }
  }
}

在 example.com 上运行的结果

alt text

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

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文