我如何使用shell脚本的浏览器本地消息传递主机应用程序 [英] How do I use a shell-script as Chrome Native Messaging host application
问题描述
你如何处理铬本地消息API调用一个bash脚本?
How do you process a Chrome Native Messaging API-call with a bash script?
我成功地使用Python与<一做href=\"http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/\"相对=nofollow>这个例子
I succeeded in doing it with python with this example
当然,我可以叫庆典
从Python code。与子
,但有可能跳过Python和处理的消息庆典
直接?
Sure I can call bash
from the python code with subprocess
, but is it possible to skip python and process the message in bash
directly?
在问题部分读取JSON序列信息到一个变量。该消息被使用JSON,UTF-8 EN codeD序列化是preceded通过标准输入本地字节序32位的消息长度。
The problematic part is reading the JSON serialized message into a variable. The message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order through stdin.
回声$ *
仅输出:铬扩展:// knldjmfmopnpolahpmmgbagdohdnhkik /
另外像
read
echo $REPLY
不输出任何东西。 JSON的消息的迹象。 Python使用 struct.unpack
这一点。可以在庆典
?
推荐答案
我建议不使用(bash)的shell脚本作为本地消息主机,因为bash的太有限了是有用的。
I suggest to not use (bash) shell scripts as a native messaging host, because bash is too limited to be useful.
读
不带任何参数读取终止前一整行,而的本地消息协议指定前四个字节指定下列消息长度(以本地字节顺序)。
read
without any parameters reads a whole line before terminating, while the native messaging protocol specifies that the first four bytes specify the length of the following message (in native byte order).
Bash是处理二进制数据的可怕工具。你的读
命令的一个改进版本将指定 -nñ
参数来停止后阅读ñ
字符(注意:不是字节)和 -r
删除一些处理。例如。以下将前四个字符存储在一个名为变量 var_ preFIX
:
Bash is a terrible tool for processing binary data. An improved version of your read
command would specify the -n N
parameter to stop reading after N
characters (note: not bytes) and -r
to remove some processing. E.g. the following would store the first four characters in a variable called var_prefix
:
IFS= read -rn 4 var_prefix
即使你假定这个存储变量中的前四个字节(不是!),那么你必须字节转换为整数。难道我已经提到的bash自动删除所有的NUL字节?这使得特征猛砸,作为一个完全有能力的本地消息主机一钱不值。
Even if you assume that this stores the first four bytes in the variable (it does not!), then you have to convert the bytes to an integer. Did I already mention that bash automatically drops all NUL bytes? This characteristics makes Bash utterly worthless for being a fully capable native messaging host.
您可以忽略前几个字节有这个缺点应对,并开始当你发现一个 {
字符时,JSON格式的请求开始分析的结果。在此之后,你必须阅读所有的输入,直到输入端找到。你需要一个JSON解析器,停止读取输入时,它遇到的JSON字符串的结尾。祝你好运与编写。
You could cope with this shortcoming by ignoring the first few bytes, and start parsing the result when you spot a {
character, the beginning of the JSON-formatted request. After this, you have to read all input until the end of the input is found. You need a JSON parser that stops reading input when it encounters the end of the JSON string. Good luck with writing that.
生成的输出是一个更简单,只需使用回声-n
或 的printf
。
Generating output is a easier, just use echo -n
or printf
.
下面是假定输入以}
,读取它(不处理)结束,并用结果回答一个最小的例子。虽然这个演示的作品,我强烈建议不要使用bash,但更丰富的(脚本)语言如Python或C ++。
Here is a minimal example that assumes that the input ends with a }
, reads it (without processing) and replies with a result. Although this demo works, I strongly recommend to not use bash, but a richer (scripting) language such as Python or C++.
#!/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
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 (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
这篇关于我如何使用shell脚本的浏览器本地消息传递主机应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!