我创建了一个扩展,它使用本地消息传递到主机。
扩展的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/"
]
}在registrey中,默认值 of HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo为C:\Users\username\Desktop\Extension1\NativeApp\manifest.json (这是清单文件物理存在的位置)。
问题是,每次我运行它时,它总是说:‘指定的本地消息主机没有找到’.我重新检查了我的代码,它似乎很好,就像google的本地消息传递指南一样。登录调试器控制台的错误是:'Uncaught Error:试图使用断开连接的端口对象‘,我不知道为什么会这样。
而且,在chrome.runtime.connectNative之后,.exe不会启动(在任务管理器中看到之后),它只是看起来像是一些与代码无关的东西,但更有可能出现在配置中。
我需要一些帮助来解决它,所以任何帮助都是有用的!
谢谢
发布于 2020-06-28 04:36:16
请注意,allowed_origins中列出的chrome扩展必须以/结束。
错误代码(没有/):
"allowed_origins": [
"chrome-extension://acajlpgjiolkocfooiankmegidcifefo"
]正确代码:
"allowed_origins": [
"chrome-extension://acajlpgjiolkocfooiankmegidcifefo/"
]发布于 2014-08-14 10:50:33
我已经想出了解决办法。我已经从头开始创建了整个包,并将主机应用程序的名称设置为小写。此外,我将注册表中的键设置为“CURRENT_USER”,并且运行良好。我想也许主机名应该是小写的,但除此之外,我不知道我哪里出错了。谢谢大家的帮助!我很感激你!
发布于 2014-08-14 08:17:43
我不确定相对路径是否适用于本机主机清单。
在任何情况下,如果您与文档中的示例相比,您使用的斜杠是错误的。
https://stackoverflow.com/questions/25303039
复制相似问题