7

我创建了一个使用本地消息传递给主机的扩展。

扩展的 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.echoC:\Users\username\Desktop\Extension1\NativeApp\manifest.json清单文件物理存在的位置)。

问题是,每次我运行它时,它一直说:“未找到指定的本地消息传递主机” ......我重新检查了我的代码,它似乎很好,就像谷歌的本地消息传递指南一样。调试器控制台中记录的错误是:“未捕获的错误:尝试使用断开连接的端口对象”,我不知道为什么它一直在发生。

此外,在chrome.runtime.connectNative.exe 之后没有启动(在任务管理器中看到之后),而且似乎有一些与代码无关的东西,但更有可能在配置中。

我需要一些帮助来解决这个问题,所以任何帮助都会很有用!

谢谢

4

3 回答 3

4

请注意,allowed_origins 中列出的 chrome 扩展名必须以 / 结尾

错误代码(不带 /):

 "allowed_origins": [
    "chrome-extension://acajlpgjiolkocfooiankmegidcifefo"
  ]

正确的代码:

 "allowed_origins": [
    "chrome-extension://acajlpgjiolkocfooiankmegidcifefo/"
  ]
于 2020-06-28T04:36:16.047 回答
0

我已经设法解决了。我再次从头开始创建了整个包,并将主机应用程序的名称设置为小写。我还在“CURRENT_USER”的注册表中设置了密钥,它运行良好。我想也许主机名应该是小写的,但除此之外我不知道我哪里出错了。非常感谢大家的帮助!!!我很感激!

于 2014-08-14T10:50:33.640 回答
0

我不确定相对路径是否适用于本机主机清单。

无论如何,如果您与docs 中的示例进行比较,则说明您使用了错误的斜线。

于 2014-08-14T08:17:43.890 回答