Google Chrome Extension - Specified Native Messaging Host Not Found

Amit Ben Ami picture Amit Ben Ami · Aug 14, 2014 · Viewed 10.5k times · Source

I created an extension that uses native messaging to a host.

The manifest.json of the extension is:

{
    "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"
    }
}

The popup.html:

    <html>
        <head>
            <script src="./main.js"></script>
        </head>
        <body>
            <button id="buttonToPress">Press</button>
        </body>
    </html>

The main.js file:

    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);
    });

I have a native application abc.exe.

The native application 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/"
        ]
    }

In the registrey, The Default Value of HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo is C:\Users\username\Desktop\Extension1\NativeApp\manifest.json (This is where the manifest file is physically exists).

The problem is, that each time i run it, it keep saying: 'Specified Native Messaging Host Not Found'... I rechecked my code and it seems to be fine, just like the google's guide of native messaging. The error that logged in the debugger's console is: 'Uncaught Error: Attempting to use a disconnected port object', which i don't know why it keeps happening.

Also, after the chrome.runtime.connectNative, the .exe doesn't start (after seeing in the task manager), and it just seems likes there something that not code-related, but more likely to be in the configuration.

I need some help in figuring it out, so any help would be usefull!

Thanks

Answer

Huy - Logarit picture Huy - Logarit · Jun 28, 2020

notice that chrome extension listed in allowed_origins has to end with /

wrong code (without /):

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

correct code:

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