问题描述
我花了两天时间阅读/尝试我能找到/想到的所有可能的解决方案,但是它们都没有起作用,我完全陷入了困境。任何方向将不胜感激!
我正在尝试通过正在开发的Chrome扩展程序读取Chrome浏览历史记录。由于此历史记录文件(一个sqlite3数据库)位于本地,因此我需要为扩展名设置本机消息以读取它。我正在使用Chrome 84,Ubuntu。我在Chrome开发者网站上关注了此example,但始终是Failed to connect: Native host has exited
。我在chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/main.html
启动此扩展程序。
-
我的本机消息传递主机(
native-messaging-example-host
文件,在本文底部下方)是用python 2编写的,但是chrome如何知道将其作为python 2运行? -
Chrome site提到
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/"
]
}
我的应用文件夹与上述example完全相同,它有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,pady=10)
self.sendButton = Tkinter.Button(self,text="Send",command=self.onSend)
self.sendButton.grid(row=1,column=1,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()
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)