我正在尝试使用Firefox的附加SDK 模拟Chrome的本机消息功能.具体来说,我正在使用child_process模块和emit方法与python子进程通信.
我能够成功地向子进程发送消息,但是我无法将消息发送回附加组件.Chrome的本机消息传递功能使用stdin/stdout.两个方向上每个消息的前4个字节表示后续消息的字节大小,因此接收方知道要读取多少.这是我到目前为止所拥有的:
var utf8 = new TextEncoder("utf-8").encode(message);
var latin = new TextDecoder("latin1").decode(utf8);
emit(childProcess.stdin, "data", new TextDecoder("latin1").decode(new Uint32Array([utf8.length])));
emit(childProcess.stdin, "data", latin);
emit(childProcess.stdin, "end");
Run Code Online (Sandbox Code Playgroud)
text_length_bytes = sys.stdin.read(4)
text_length = struct.unpack('i', text_length_bytes)[0]
text = sys.stdin.read(text_length).decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
sys.stdout.write(struct.pack('I', len(message)))
sys.stdout.write(message)
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
这是我在努力的地方.当长度小于255时,我可以使用它.例如,如果长度为55,则可以:
childProcess.stdout.on('data', (data) => { // data is '7' (55 UTF-8 encoded)
var utf8Encoded = new TextEncoder("utf-8).encode(data);
console.log(utf8Encoded[0]); // 55
}
Run Code Online (Sandbox Code Playgroud)
但是,就像我说的那样,它并不适用于所有数字.我确定我必须对TypedArrays做些什么,但我很难把所有东西放在一起.
javascript python firefox-addon firefox-addon-sdk chrome-native-messaging
我能够以某种方式运行这个扩展:
https://github.com/mdn/webextensions-examples/tree/master/beastify
popup/choose_beast.js单击时会调用浏览器图标。
有人可以告诉我为什么这段代码(放置在顶部popup/choose_beast.js)会生成异常:
try{
var ss = require("sdk/simple-storage");
ss.storage.myArray = [1, 1, 2, 3, 5, 8, 13];
}catch(e){
alert('exception');
console.log(e);
}
Run Code Online (Sandbox Code Playgroud)
这是 中的相关条目manifest.json:
"browser_action": {
"default_icon": "icons/beasts-32.png",
"default_title": "Beastify",
"default_popup": "popup/choose_beast.html"
}
Run Code Online (Sandbox Code Playgroud)
我可以通过什么方式将数据存储在此弹出 html 中,以便我可以随时在内容脚本中检索?
另外,这个页面choose_beast.html是在什么上下文中运行的?背景、页面脚本还是内容脚本?