【问题标题】:Google Chrome Extension - Specified Native Messaging Host Not FoundGoogle Chrome 扩展程序 - 未找到指定的本机消息传递主机
【发布时间】: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 没有启动(在任务管理器中看到后),而且似乎有一些与代码无关的东西,但更有可能在配置中。

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

谢谢

【问题讨论】:

标签: javascript json google-chrome-extension chrome-native-messaging


【解决方案1】:

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

错误代码(不带 /):

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

正确的代码:

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

【讨论】:

    【解决方案2】:

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

    无论如何,如果您与example in the docs 进行比较,您使用的斜线类型是错误的。

    【讨论】:

    • 当我只给出“abc.exe”或完整路径时,这也不起作用
    • 您是否将完整路径指定为C:\\Users\\s8018942\\Desktop\\Extension1\\NativeApp\\abc.exe?也可以为注册表项尝试相同的双反斜杠格式。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多