如何在 Chrome Native Messaging 主机上解析来自 stdin 的 JSON?

2024-01-10

有关的:

  • 如何在本机消息传递主机上解析来自标准输入的 JSON? https://stackoverflow.com/questions/48385086/how-to-parse-json-from-stdin-at-native-messaging-host

使用以下代码如何使用 shell 脚本作为 Chrome Native Messaging 主机应用程序 https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application发送JSON从客户端(应用程序)到主机

#!/bin/bash
# Loop forever, to deal with chrome.runtime.connectNative
while IFS= read -r -n1 c; do
    # Read the first message
    # Assuming that the message ALWAYS ends with a },
    # with no }s in the string. Adopt this piece of code if needed.
    if [ "$c" != '}' ] ; then
        continue
    fi
    # do stuff
    message='{"message": "'$c'"}'
    # Calculate the byte size of the string.
    # NOTE: This assumes that byte length is identical to the string length!
    # Do not use multibyte (unicode) characters, escape them instead, e.g.
    # message='"Some unicode character:\u1234"'
    messagelen=${#message}

    # Convert to an integer in native byte order.
    # If you see an error message in Chrome's stdout with
    # "Native Messaging host tried sending a message that is ... bytes long.",
    # then just swap the order, i.e. messagelen1 <-> messagelen4 and
    # messagelen2 <-> messagelen3
    messagelen1=$(( ($messagelen      ) & 0xFF ))               
    messagelen2=$(( ($messagelen >>  8) & 0xFF ))               
    messagelen3=$(( ($messagelen >> 16) & 0xFF ))               
    messagelen4=$(( ($messagelen >> 24) & 0xFF ))               

    # Print the message byte length followed by the actual message.
    printf "$(printf '\\x%x\\x%x\\x%x\\x%x' \
        $messagelen1 $messagelen2 $messagelen3 $messagelen4)%s" "$message"

done

这导致

{"message":"}"}

在客户端应用程序中收到。

循环显然没有捕获变量$cwhile loop.

使用来自客户端的 JSON 输入

{"text":"abc"}

使用 JavaScript,我们可以通过检查前后索引处的字符来获取 JSON 字符串的单个属性

var result = "";
var i = 0;
var input = '{"text":"abc"}';
while (i < input.length) {

  if (input[i - 2] === ":" && input[i - 1] === '"' || result.length) {
    result += input[i];
  }

  if (input[i + 1] === '"' && input[i + 2] === "}") {
    // do stuff
    break;
  }
  
  ++i;
  
};

console.log(result);

但不确定如何转换if从 JavaScript 执行 bash 的条件,以获取上面的代码将匹配并连接到后面的字符串的单个 JSON 属性如何在 Bash 的“if”语句中比较两个字符串变量? https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash and 如何在 Bash 中连接字符串变量? https://stackoverflow.com/questions/4181703/how-to-concatenate-string-variables-in-bash。如果 JSON 对象具有使用嵌套或多个 if 条件的嵌套属性,则可能需要针对传递到主机的特定 JSON 多次调整代码。

The 文档 https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-protocol描述协议

本机消息传递协议

Chrome 在单独的进程中启动每个本机消息传递主机,并且 使用标准输入 (stdin) 和标准输出与其进行通信 (标准输出)。双向发送消息使用相同的格式: 每条消息均使用 JSON、UTF-8 编码进行序列化,并置于前面 具有本机字节顺序的 32 位消息长度。

无法在 SO 中找到任何答案,该答案明确描述了使用 bash 从 Chrome Native Messsaging 应用程序主机上的标准输入解析 JSON 的过程。

如何创建通用解决方案或算法(包括协议和语言所需的每个步骤的描述)来解析从主机客户端发送的 JSONstdin,获取并设置将发送的 JSON 响应消息的本机字节顺序的正确 32 位消息长度(例如,echo; printf)使用 bash 到客户端?


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Chrome Native Messaging 主机上解析来自 stdin 的 JSON? 的相关文章

随机推荐