Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome Extension - Specified Native Messaging Host Not Found

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

like image 682
Amit Ben Ami Avatar asked Aug 14 '14 08:08

Amit Ben Ami


Video Answer


2 Answers

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/"
  ]
like image 73
Huy - Logarit Avatar answered Oct 06 '22 00:10

Huy - Logarit


I've managed to work the solution out. I've created the whole pack once again from scratch and set the name of the host application in lowercase. Also i set the key in the registry in 'CURRENT_USER' and it worked well. I guess that maybe the host name should be in lowercase but other than this I don't know where I went wrong. Thanks alot guys for the help!!! I Appreciate it!

like image 32
Amit Ben Ami Avatar answered Oct 05 '22 23:10

Amit Ben Ami