我创建了一个使用本地消息传递给主机的扩展。
扩展的 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);
});
我有一个本机应用程序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
清单文件物理存在的位置)。
问题是,每次我运行它时,它一直说:“未找到指定的本地消息传递主机” ......我重新检查了我的代码,它似乎很好,就像谷歌的本地消息传递指南一样。调试器控制台中记录的错误是:“未捕获的错误:尝试使用断开连接的端口对象”,我不知道为什么它一直在发生。
此外,在chrome.runtime.connectNative
.exe 之后没有启动(在任务管理器中看到之后),而且似乎有一些与代码无关的东西,但更有可能在配置中。
我需要一些帮助来解决这个问题,所以任何帮助都会很有用!
谢谢