Javascript “如何修复”;“本机主机已退出”;对于Chrome扩展本机消息?
我花了两天时间阅读/尝试我能找到/想到的所有可能的解决方案,但没有一个有效,我完全被卡住了。任何方向都将不胜感激 我试图通过我正在开发的Chrome扩展来阅读Chrome浏览历史。由于这个历史文件(sqlite3数据库)位于本地,因此我需要为我的扩展设置本机消息来读取它。我使用的是Chrome84,Ubuntu。我在Chrome开发者网站上遵循了这一点,但它总是Javascript “如何修复”;“本机主机已退出”;对于Chrome扩展本机消息?,javascript,json,google-chrome,google-chrome-extension,chrome-native-messaging,Javascript,Json,Google Chrome,Google Chrome Extension,Chrome Native Messaging,我花了两天时间阅读/尝试我能找到/想到的所有可能的解决方案,但没有一个有效,我完全被卡住了。任何方向都将不胜感激 我试图通过我正在开发的Chrome扩展来阅读Chrome浏览历史。由于这个历史文件(sqlite3数据库)位于本地,因此我需要为我的扩展设置本机消息来读取它。我使用的是Chrome84,Ubuntu。我在Chrome开发者网站上遵循了这一点,但它总是无法连接:本机主机已退出。我在chrome上启动了这个扩展-extension://knldjmfmopnpolahpmmgbagdoh
无法连接:本机主机已退出。我在chrome上启动了这个扩展-extension://knldjmfmopnpolahpmmgbagdohdnhkik/main.html
我的本机消息传递主机(本机消息传递示例主机
文件,见本文底部)是用Python2编写的,但是chrome如何知道将其作为Python2运行呢
本机消息传递主机如何将消息发送到我的公开页面?我阅读了它的代码,但我不熟悉它使用的所有模块和线程
Chrome提到,本机主机已退出
可能是因为“在Chrome读取消息之前,本机消息主机的管道已断开。这很可能是由您的本机消息主机启动的”,但没有提及如何修复它。我尝试(1)将其权限从-rwxr--r--
更改为-rwxrwx
,(2)将JSON文件移动到更容易访问的文件夹,并相应地更改路径
值。我的问题是,导致“在Chrome读取消息之前,本机消息主机的管道断开”的可能原因是什么
My/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/"
]
}
我的应用程序文件夹与上面提到的完全相同,它有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();
});
谢谢你 1)JSON只是定位主机应用程序所需的信息文件。2) 请尝试根据检查chrome_debug.log。不要忘记完全退出所有Chrome进程,例如使用浏览器菜单中的exit命令。谢谢@wOxxOm,我不确定这是否是正常行为:当我在终端中打开google chrome
时,浏览器确实会打开,但google chrome
在说NaCl helper进程运行时没有沙箱后立即停止!您很可能需要正确配置SUID沙箱
。我从--enable logging=stderr--v=1>log.txt 2>&1中读取了log.txt
,它似乎不受我与扩展交互的操作的影响。你认为我需要解决沙箱问题吗?或者我还可以研究其他东西?NaCl是另一种你不用的插件系统,所以它不会影响你。检查chrome_debug.log以获取线索。谢谢@沃克索姆。我在浏览器中使用退出关闭了所有Chrome窗口,然后运行google Chrome-enable logging=stderr--v=1>log.txt 2>&1
,但是Chrome_debug.log中没有任何内容,它似乎根本没有更新,最近的更改发生在2小时前。我注意到还有另一个文件名为chrome\u shutdown\u ms.txt
,它在我每次运行chrome时都会更新,但又是一个空文件。这可能太多了,无法在这里的评论中得到帮助,我将继续阅读这篇文章。非常感谢你!Hi@wOxxOm,一个简短的问题,您认为我的本机消息传递主机有问题吗?它是用Python2编写的,我刚刚在问题的底部添加了它的脚本。我与tkinter窗口的交互运行平稳,没有错误信息。我试着注释掉sys.exit(0)
,但是运气不好。