javascript - 如何修复 Chrome 扩展原生消息的“原生主机已退出”?
问题描述
我花了两天时间阅读/尝试我能找到/想到的所有可能的解决方案,但它们都没有奏效,我完全被困住了。任何方向将不胜感激!
我正在尝试通过正在开发的 Chrome 扩展程序读取 Chrome 浏览历史记录。由于这个历史文件(一个 sqlite3 数据库)位于本地,我需要为我的扩展设置本地消息传递来读取它。我正在使用 Chrome 84,Ubuntu。我从 Chrome 开发者网站上遵循了这个例子,但它总是Failed to connect: Native host has exited
. 我在chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/main.html
.
我的本机消息传递主机(
native-messaging-example-host
文件,位于本文底部)是用 python 2 编写的,但是 chrome 是如何知道将其作为 python 2 运行的呢?本机消息传递主机如何向我公开的页面发送消息?我阅读了它的代码,但我并不熟悉它使用的所有模块和线程。
Chrome网站提到这
Native host has exited
可能是由于“在 Chrome 读取消息之前,到本机消息传递主机的管道已损坏。这很可能是从您的本机消息传递主机发起的。”,但它没有提到如何修复它。我尝试(1)将其权限从 更改-rwxr--r--
为-rwxrwxrwx
,(2)将 JSON 文件移动到更容易访问的文件夹并path
相应地更改值。我的问题是,导致“在 Chrome 读取消息之前,到本机消息传递主机的管道已损坏”的可能原因是什么?
我的 /home/username/.config/google-chrome/NativeMessagingHosts/com.google.chrome.example.echo.json:
{
"name": "com.google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "/home/moon/BrowserExtensions/nativeMessage/host/native-messaging-example-host",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
我的 app 文件夹与上面提到的示例完全相同,它有 4 个文件:icon-128.png
main.html
main.js
manifest.json
. manifest.json
: _
{
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Native Messaging Example",
"version": "1.0",
"manifest_version": 2,
"description": "Send a message to a native application.",
"app": {
"launch": {
"local_path": "main.html"
}
},
"icons": {
"128": "icon-128.png"
},
"permissions": [
"nativeMessaging"
]
}
main.js
文件:
var port = null;
var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
function appendMessage(text) {
document.getElementById('response').innerHTML += "<p>" + text + "</p>";
}
function updateUiState() {
if (port) {
document.getElementById('connect-button').style.display = 'none';
document.getElementById('input-text').style.display = 'block';
document.getElementById('send-message-button').style.display = 'block';
} else {
document.getElementById('connect-button').style.display = 'block';
document.getElementById('input-text').style.display = 'none';
document.getElementById('send-message-button').style.display = 'none';
}
}
function sendNativeMessage() {
message = {"text": document.getElementById('input-text').value};
port.postMessage(message);
appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
}
function onNativeMessage(message) {
appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
}
function onDisconnected() {
appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
port = null;
updateUiState();
}
function connect() {
var hostName = "com.google.chrome.example.echo";
appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
updateUiState();
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('connect-button').addEventListener(
'click', connect);
document.getElementById('send-message-button').addEventListener(
'click', sendNativeMessage);
updateUiState();
});
native-messaging-example-host
文件:
import struct
import sys
import threading
import Queue
try:
import Tkinter
import tkMessageBox
except ImportError:
Tkinter = None
# On Windows, the default I/O mode is O_TEXT. Set this to O_BINARY
# to avoid unwanted modifications of the input/output streams.
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Helper function that sends a message to the webapp.
def send_message(message):
# Write message size.
sys.stdout.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
# Thread that reads messages from the webapp.
def read_thread_func(queue):
message_number = 0
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) == 0:
if queue:
queue.put(None)
sys.exit(0)
# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
# Read the text (JSON object) of the message.
text = sys.stdin.read(text_length).decode('utf-8')
if queue:
queue.put(text)
else:
# In headless mode just send an echo message back.
send_message('{"echo": %s}' % text)
if Tkinter:
class NativeMessagingWindow(Tkinter.Frame):
def __init__(self, queue):
self.queue = queue
Tkinter.Frame.__init__(self)
self.pack()
self.text = Tkinter.Text(self)
self.text.grid(row=0, column=0, padx=10, pady=10, columnspan=2)
self.text.config(state=Tkinter.DISABLED, height=10, width=40)
self.messageContent = Tkinter.StringVar()
self.sendEntry = Tkinter.Entry(self, textvariable=self.messageContent)
self.sendEntry.grid(row=1, column=0, padx=10, pady=10)
self.sendButton = Tkinter.Button(self, text="Send", command=self.onSend)
self.sendButton.grid(row=1, column=1, padx=10, pady=10)
self.after(100, self.processMessages)
def processMessages(self):
while not self.queue.empty():
message = self.queue.get_nowait()
if message == None:
self.quit()
return
self.log("Received %s" % message)
self.after(100, self.processMessages)
def onSend(self):
text = '{"text": "' + self.messageContent.get() + '"}'
self.log('Sending %s' % text)
try:
send_message(text)
except IOError:
tkMessageBox.showinfo('Native Messaging Example',
'Failed to send message.')
sys.exit(1)
def log(self, message):
self.text.config(state=Tkinter.NORMAL)
self.text.insert(Tkinter.END, message + "\n")
self.text.config(state=Tkinter.DISABLED)
def Main():
if not Tkinter:
send_message('"Tkinter python module wasn\'t found. Running in headless ' +
'mode. Please consider installing Tkinter."')
read_thread_func(None)
sys.exit(0)
queue = Queue.Queue()
main_window = NativeMessagingWindow(queue)
main_window.master.title('Native Messaging Example')
thread = threading.Thread(target=read_thread_func, args=(queue,))
thread.daemon = True
thread.start()
main_window.mainloop()
sys.exit(0)
if __name__ == '__main__':
Main()
谢谢!!!
解决方案
推荐阅读
- python-3.x - 在 python 中处理 USR1 信号
- css - 如何在关键帧动画后隐藏元素
- python-3.x - 我正在尝试使用 subprocces 向 spi.exe 发送一些命令
- python - Fitting empirical distribution to theoretical ones with Scipy (Python) with bounds of parameters
- tensorflow-lite - 尝试使用 TFlite 运行推理时出错
- sql - 正则表达式否定后缀否定环视不起作用
- pandas - Oracle中无法识别的表
- java - Java JsonIgnore 没有隐藏字段
- javascript - 如何将 POST WMS 请求中的 base64 图像与 ImageMapType getTileUrl 一起使用?
- cryptocurrency - 如何在 TronLink 钱包中显示我的硬币/代币的价格