我创建了一个使用本地消息传递给主机的扩展。
扩展的 manifest.json 是:
{
"manifest_version": 2,
"version": "1.0",
"name": "Native Messaging Example",
"description": "Send a message to a native application",
"permissions": [
"nativeMessaging"
],
"browser_action": {
"default_popup": "popup.html"
}
}
popup.html:
<html>
<head>
<script src="./main.js"></script>
</head>
<body>
<button id="buttonToPress">Press</button>
</body>
</html>
main.js 文件:
var port = null;
function connect() {
port = chrome.runtime.connectNative('com.google.chrome.example.echo');
port.onMessage.addListener(function(message) {
alert(message);
port.disconnect();
});
port.onDisconnect.addListener(function() {
port = null;
alert(chrome.runtime.lastError.message);
});
var message = {
'filePath': 'C:\\Users\\username\\Desktop\\themes\\Wallpaper\\Architecture\\img13.jpg'
};
port.postMessage(message);
}
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('buttonToPress').addEventListener('click', connect);
});
我有一个 native 应用程序
abc.exe
.原生应用 manifest.json:
{
"name": "com.google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "./abc.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extensions://fegpbklgdffjmfjmhknpmgepbddbcghk/"
]
}
在注册表中,默认值 的
HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo
是 C:\Users\username\Desktop\Extension1\NativeApp\manifest.json
(这是 list 文件物理存在的地方)。问题是,每次我运行它时,它都会说: '未找到指定的 native 消息传递主机' ...我重新检查了我的代码,它似乎很好,就像谷歌的本地消息传递指南一样。调试器控制台中记录的错误是: ' Uncaught Error :尝试使用断开连接的端口对象' ,我不知道为什么它一直在发生。
此外,在
chrome.runtime.connectNative
之后,.exe 没有启动(在任务管理器中看到之后),而且似乎有一些与代码无关的东西,但更有可能在配置中。我需要一些帮助来解决这个问题,所以任何帮助都会很有用!
谢谢
最佳答案
请注意,allowed_origins 中列出的 chrome 扩展名必须以/结尾
错误代码(不带/):
"allowed_origins": [
"chrome-extension://acajlpgjiolkocfooiankmegidcifefo"
]
正确的代码: "allowed_origins": [
"chrome-extension://acajlpgjiolkocfooiankmegidcifefo/"
]
关于javascript - Google Chrome 扩展程序 - 未找到指定的 native 消息传递主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25303039/