栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Shell脚本作为Chrome Native Messaging主机应用程序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Shell脚本作为Chrome Native Messaging主机应用程序

我建议不要将(bash)shell脚本用作本机消息传递主机,因为bash太有限而无用。

read
不带任何参数的参数将在终止之前读取整行,而本机消息传递协议指定前四个字节指定后续消息的长度(以本机字节顺序)。

Bash是用于处理二进制数据的可怕工具。

read
命令的改进版本将指定
-nN
参数,以在
N
字符(注意:不是字节)之后停止读取并
-r
删除某些处理。例如,以下代码将前四个字符存储在名为的变量中
var_prefix

IFS= read -rn 4 var_prefix

即使您假定此方法将前四个字节存储在变量中(不是!),也必须将字节转换为整数。我是否已经提到过bash会自动丢弃所有NUL字节?这种特性使Bash成为完全有能力的本机消息传递主机时毫无用处。

您可以通过忽略前几个字节来解决此缺点,并在发现

{
字符(JSON格式请求的开始)时开始解析结果。此后,您必须阅读所有输入,直到找到输入的结尾。您需要一个JSON解析器,该解析器在遇到JSON字符串的结尾时会停止读取输入。祝你好运。

只需使用

echo -n
或即可生成输出
printf

这是一个最小的示例,假定输入以a结尾

}
,读取它(不进行处理)并返回结果。尽管此演示有效,但我
强烈建议不要使用bash,而应使用更丰富的(脚本)语言,例如Python或C ++。

#!/bin/bash# Loop forever, to deal with chrome.runtime.connectNativewhile 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 pre if needed.    if [ "$c" != '}' ] ; then        continue    fi    message='{"message": "Hello world!"}'    # Calculate the byte size of the string.    # NOTE: This assumes that byte length is identical to the string length!    # Do not use multibyte (unipre) characters, escape them instead, e.g.    # message='"Some unipre 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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569149.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号