11

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

Amit Ben Ami
  • 548
  • 2
  • 6
  • 21
  • possible duplicate of [Chrome extension native messaging got error:"Specified native messaging host not found."](http://stackoverflow.com/questions/21181265/chrome-extension-native-messaging-got-errorspecified-native-messaging-host-not) – Xan Aug 14 '14 at 08:26
  • 1
    I'm not sure if there's yet support for HKCU (https://code.google.com/p/chromium/issues/detail?id=237873). Insert the registry value into HKLM and see if it helps. – Almog G. Aug 14 '14 at 08:44
  • It doesn't work also with the local machine key.. – Amit Ben Ami Aug 14 '14 at 08:49
  • Did you see my "duplicate" comment above? Can you make a screenshot of your regedit or export the key and quote it here to be sure it's the right format? – Xan Aug 14 '14 at 09:36

3 Answers3

6

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/"
  ]
Huy - Logarit
  • 634
  • 8
  • 13
3

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!

Amit Ben Ami
  • 548
  • 2
  • 6
  • 21
0

I'm not sure relative paths work for Native Host manifests.

In any case, if you compare with the example in the docs, you're using the wrong kind of slash.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • This also doesn't work when i only give the "abc.exe", or the full path – Amit Ben Ami Aug 14 '14 at 08:21
  • Did you give the full path as `C:\\Users\\s8018942\\Desktop\\Extension1\\NativeApp\\abc.exe`? Try also the same double-backslash format for the registry key. – Xan Aug 14 '14 at 08:23