我尝试使用Firefox56附加组件在Linux-Debian-Stretch-64位下向python3程序发送消息。使用port.postMessage()函数它可以工作,但我需要有异步程序所以我使用sendNativeMessage(),但即使程序工作,我有消息:
本机应用程序试图发送一个1990837074字节的消息,其中 超过1048576字节的限制。 NativeMessaging.jsm:283 _startRead / this.readPromise< 资源://gre/modules/NativeMessaging.jsm:283:11
我的后台计划:
var port = browser.runtime.connectNative("gettext");
var first = 0
var portFromCS;
function onExecuted(result) {
console.log(`appel au content-script fait`);
}
function onError(error) {
console.log(`Error: ${error}`);
}
browser.commands.onCommand.addListener(function(command) {
if (command == "toggle-feature") {
console.log("toggling the feature!");
var executing = browser.tabs.executeScript({ file: "/content-script.js", allFrames: false });
executing.then(onExecuted, onError);
}
});
function onSend(response) {
console.log("recu du python" + response);
}
function onBug(error) {
console.log(`Error python: ${error}`);
}
function connected(p) {
portFromCS = p;
portFromCS.onMessage.addListener(function(m) {
console.log("longueur message = " + m.length)
if ( m.length > 1 && m.charAt(0) > 'а' && m.length < 100) {
var sending = browser.runtime.sendNativeMessage("gettext",m);
sending.then(onSend, onBug);
}
});
}
browser.runtime.onConnect.addListener(connected);
/*
port.onMessage.addListener((response) => {
console.log("Recu de python" + response);
});*/
应用代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Note that running python with the `-u` flag is required on Windows,
# in order to ensure that stdin and stdout are opened in binary, rather
# than text, mode.
import sys, json, struct, codecs, subprocess, pymorphy2
# Read a message from stdin and decode it.
def getMessage():
rawLength = sys.stdin.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.read(messageLength)
return json.loads(message)
# Encode a message for transmission, given its content.
def encodeMessage(messageContent):
encodedContent = json.dumps(messageContent)
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}
# Send an encoded message to stdout.
def sendMessage(encodedMessage):
sys.stdout.write(encodedMessage['length'])
sys.stdout.write(encodedMessage['content'])
sys.stdout.flush()
morph = pymorphy2.MorphAnalyzer()
while True:
receivedMessage = ""
receivedMessage = getMessage()
内容脚本:
var selectedText = getSelection().toString();
var myPort = browser.runtime.connect({name:"port-from-cs"});
myPort.postMessage(selectedText);
的manifest.json:
{
"description": "Native messaging and Hotkey and content-script messaging",
"manifest_version": 2,
"name": "getSelectedTextFromHotkey",
"version": "1.0",
"icons": {
"48": "icons/message.svg"
},
"applications": {
"gecko": {
"id": "gettext@example.org",
"strict_min_version": "50.0"
}
},
"background": {
"scripts": ["background.js"]
},
"commands": {
"toggle-feature": {
"suggested_key": {
"default": "Ctrl+Shift+4",
"linux": "Ctrl+Shift+5"
},
"description": "Send the selected text"
}
},
"browser_action": {
"default_icon": "icons/message.svg"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content-script.js"]
}
],
"permissions": [ "<all_urls>","nativeMessaging","webRequest"]
}
python程序提供正确的信息,不向后台程序发送信息。所以,我认为问题来自background.js
你知道错误在哪里吗?
答案 0 :(得分:0)
迟到总比不到好。
因为您的应用程序不回复消息,所以Firefox会读取整个标准输入缓冲区(或其他内容)以搜索有效的响应消息。它读取的缓冲区超过最大大小(可能由您的操作系统决定)。由于它仍然找不到有效的消息,因此Firefox认为该应用程序已发送大量消息并引发错误。
这也可能是由于响应无效消息引起的。
解决方案:回复有效的邮件(即用encodeMessage
编码并用sendMessage
发送)