Javascript 如何在Firefox插件(如Chrome本机消息传递)中传递子进程消息
我正在尝试使用Firefox的附加SDK来模拟Chrome。具体地说,我正在使用和与python子进程通信 我能够成功地将消息发送到子进程,但无法将消息发送回加载项。Chrome的本机消息功能使用标准输入/标准输出。两个方向上每条消息的前4个字节表示后续消息的字节大小,以便接收方知道要读取多少。以下是我目前掌握的情况: 附加到子进程 来自加载项的子进程(Python) 要添加的子进程 来自子进程的加载项 这就是我挣扎的地方。我让它在长度小于255时工作。例如,如果长度为55,则可以:Javascript 如何在Firefox插件(如Chrome本机消息传递)中传递子进程消息,javascript,python,firefox-addon,firefox-addon-sdk,chrome-native-messaging,Javascript,Python,Firefox Addon,Firefox Addon Sdk,Chrome Native Messaging,我正在尝试使用Firefox的附加SDK来模拟Chrome。具体地说,我正在使用和与python子进程通信 我能够成功地将消息发送到子进程,但无法将消息发送回加载项。Chrome的本机消息功能使用标准输入/标准输出。两个方向上每条消息的前4个字节表示后续消息的字节大小,以便接收方知道要读取多少。以下是我目前掌握的情况: 附加到子进程 来自加载项的子进程(Python) 要添加的子进程 来自子进程的加载项 这就是我挣扎的地方。我让它在长度小于255时工作。例如,如果长度为55,则可以: child
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
}
但是,正如我所说,它并不适用于所有数字。我肯定我必须用TypeDarray做些什么,但我正在努力把所有的东西放在一起。也许这与这个问题类似: 仅限Windows:确保程序的I/O模式设置为O_二进制。默认情况下,I/O模式为O_文本,这会破坏消息格式,因为换行符(\n=0A)被替换为Windows样式的行尾(\r\n=0D 0A)。可以使用设置I/O模式
这里的问题是,Firefox试图在默认情况下将标准输出读取为UTF-8流。由于UTF-8不使用完整的第一个字节,因此会出现损坏的字符,例如255。解决方案是告诉Firefox以二进制编码读取,这意味着您以后必须手动解析实际的消息内容
var childProcess = spawn("mybin", [ '-a' ], { encoding: null });
你的听众会像这样工作
var decoder = new TextDecoder("utf-8");
var readIncoming = (data) => {
// read the first four bytes, which indicate the size of the following message
var size = (new Uint32Array(data.subarray(0, 4).buffer))[0];
//TODO: handle size > data.byteLength - 4
// read the message
var message = decoder.decode(data.subarray(4, size));
//TODO: do stuff with message
// Read the next message if there are more bytes.
if(data.byteLength > 4 + size)
readIncoming(data.subarray(4 + size));
};
childProcess.stdout.on('data', (data) => {
// convert the data string to a byte array
// The bytes got converted by char code, see https://dxr.mozilla.org/mozilla-central/source/addon-sdk/source/lib/sdk/system/child_process/subprocess.js#357
var bytes = Uint8Array.from(data, (c) => c.charCodeAt(0));
readIncoming(bytes);
});
如果不将其编码发送到utf8会怎么样?在另一侧进行编码吗?在OS.File中,我们只需要处理arraybuffer,并在末尾处理编码。谢谢。我认为这是一些有价值的信息,但不适用于这个特定的问题。首先,我不在Windows上。其次,我正在将正确的数据返回到加载项,只是格式错误(默认情况下,字符编码为UTF-8,而不是原始字节)。这没有帮助,因为Firefox继续将utf8编码的数据发送到EXE。@AlexanderDyagilev此解决方案与发送位无关,它描述了如何解码传入流。请参阅发送位的问题(“附加到子进程”)。但我建议现在就使用或至少使用一个,避免使用sdk/子进程
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
}
var childProcess = spawn("mybin", [ '-a' ], { encoding: null });
var decoder = new TextDecoder("utf-8");
var readIncoming = (data) => {
// read the first four bytes, which indicate the size of the following message
var size = (new Uint32Array(data.subarray(0, 4).buffer))[0];
//TODO: handle size > data.byteLength - 4
// read the message
var message = decoder.decode(data.subarray(4, size));
//TODO: do stuff with message
// Read the next message if there are more bytes.
if(data.byteLength > 4 + size)
readIncoming(data.subarray(4 + size));
};
childProcess.stdout.on('data', (data) => {
// convert the data string to a byte array
// The bytes got converted by char code, see https://dxr.mozilla.org/mozilla-central/source/addon-sdk/source/lib/sdk/system/child_process/subprocess.js#357
var bytes = Uint8Array.from(data, (c) => c.charCodeAt(0));
readIncoming(bytes);
});