【发布时间】:2014-08-14 08:07:33
【问题描述】:
我创建了一个使用本地消息传递给主机的扩展。
扩展的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(这是清单文件实际存在的位置)。
问题是,每次我运行它时,它一直说:'找不到指定的本地消息传递主机'...我重新检查了我的代码,它似乎很好,就像google 的本地消息传递指南。调试器控制台中记录的错误是:'Uncaught Error: Attempting to use a disconnected port object',我不知道为什么它一直在发生。
另外,在chrome.runtime.connectNative 之后,.exe 没有启动(在任务管理器中看到后),而且似乎有一些与代码无关的东西,但更有可能在配置中。
我需要一些帮助来解决这个问题,所以任何帮助都会很有用!
谢谢
【问题讨论】:
-
我不确定是否还支持 HKCU (code.google.com/p/chromium/issues/detail?id=237873)。将注册表值插入 HKLM,看看是否有帮助。
-
它也不适用于本地机器密钥..
-
你看到我上面的“重复”评论了吗?您能否截取您的 regedit 或导出密钥并在此处引用以确保格式正确?
标签: javascript json google-chrome-extension chrome-native-messaging