1

This question is a continuation of that one, because there hasn't been any answer with Nodejs code in that question, 6 years now.

I use Chrome 79 and Nodejs 13, in windows 10.

I have a Nodejs script(see below) acting as the native messaging host, and a Chrome extension.
I want to send messages from the Nodejs script to my extension.

I know how to receive messages in my Chrome extension - here is its background.js:

var port = chrome.runtime.connectNative('my_messaging_host');

port.onMessage.addListener((message) => {
  console.log("Received: " + message);
});

The Nodejs script I have(see below) is the native messaging host. I used as a reference the only Nodejs messaging host code example I could find, from MDN.
I've noticed that this Nodejs code example was added in that MDN wiki page very recently, 10 days ago, and it has issues/needs improvement.

Anyway, I've modified it as follows, but I'm getting the following error from my extension when sending messages: Failed to connect: Error when communicating with the native messaging host, which, based on the docs indicates an incorrect implementation of the communication protocol in the native messaging host.

So, why is my Nodejs script not working ok?
Could you please give me a working Nodejs host code example?

function sendMessage(msg) {
    var buffer = Buffer.from(JSON.stringify(msg));

    var header = Buffer.alloc(4);
    header.writeUInt32LE(buffer.length, 0);

    var data = Buffer.concat([header, buffer]);
    process.stdout.write(data);
}

For reference, various native messaging host examples:

darkred
  • 591
  • 5
  • 28
  • I hope you're following the guide (https://developer.chrome.com/extensions/nativeMessaging#examples) thoroughly, like adding registry keys, right? Please confirm. – IamAshKS Dec 14 '19 at 18:52
  • Also, did you tried Google's example: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging – IamAshKS Dec 14 '19 at 18:57
  • @Ashutosh Yes, I'm following the guide exactly. And yes, I've tried the Google's example and it's working but the host in that example (the 'native-messaging-example-host' file) is irrelevant because it's written in Python, while I want one in NodeJS. – darkred Dec 14 '19 at 19:15
  • Chrome's Native Messaging suggests that `Error when communicating with the native messaging host.` is due to *an incorrect implementation of the communication protocol in the native messaging host.* But, I can't see such an error in your code. Could you use `sendMessage()` without `decodeMessage()` to send a dummy message, say `sendMessage("Hello")`? Maybe you already tried it? – IamAshKS Dec 14 '19 at 19:31
  • @AshutoshKS I've noticed that the Nodejs code example I used as a reference was added in that MDN wiki page very recently, 10 days ago, and I think it has issues/needs improvement. I've updated the question accordingly. – darkred Dec 15 '19 at 18:19

1 Answers1

2

I've fixed the sendMessage function from the MDN wiki page as follows, i.e.:

  • no need allocating a new Buffer via stringifying the string/wrongly supposed as JSON, msg
  • the 1st of the 4 bytes of the header (converted in little-endian) should take the length of the given msg as string
  • the msg should be send using stout as it is, i.e. as string
function sendMessage(msg) {

    var header = Buffer.alloc(4);
    header.writeUInt32LE(msg.length, 0);

    process.stdout.write(header);
    process.stdout.write(msg);

}

Also, here is an example message to send:

sendMessage('{"test": "content"}');

 

PS: I also updated that MDN wiki page with it.

darkred
  • 591
  • 5
  • 28
  • Hey @darkred could you please provide a full example with Node.js? as MDN version is not working, at least I'm not able to make it working... – D.Dimitrioglo Oct 28 '20 at 20:34
  • I had updated the example in MDN wiki page as follows: diff: https://wiki.developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging$compare?locale=en-US&to=1595872&from=1593969 revision: https://wiki.developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging$revision/1595872. That example worked fine for me. (as you may see in the the wiki page history, it has been changed 8 times by other MDN contributors since my edit) – darkred Oct 28 '20 at 22:35
  • Thank you, for the link, will check it, but looks much more readable than official one! – D.Dimitrioglo Oct 29 '20 at 10:26