I have Chrome extension that uses runtime.connectNative and runtime.sendMessage to communicate to my simple Node JS app that listens to any message from the extension.
The problem is, it doesn't work in my M2 Macbook but works in my Intel Macbook.
Here's my index.js:
#!/usr/local/bin/node
var nativeMessage = require("chrome-native-messaging");
process.stdin
.pipe(new nativeMessage.Input())
.pipe(
new nativeMessage.Transform(function (msg, push, done) {
var reply = "pong"; // getReplyFor(msg); // Implemented elsewhere by you.
push(reply); // Push as many replies as you like.
done(); // Call when done pushing replies.
})
)
.pipe(new nativeMessage.Output())
.pipe(process.stdout);
which is taken from this library https://github.com/jdiamond/chrome-native-messaging/#api
Here is my service worker called background.js:
var port = chrome.runtime.connectNative("com.my_company.my_application");
port.onMessage.addListener(function (msg) {
console.log("Received message: " + msg);
});
port.onDisconnect.addListener(function () {
console.log("Disconnected");
});
port.postMessage({ text: "Hello, user" });
I also noticed that with M2 Macbook, my Chrome host file location is in /Users/<user>/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/
While my Intel Macbook host file location is in /Library/Google/Chrome/NativeMessagingHosts/
Not sure if that path difference has something to do with my issue but do I need to set some kind of file/folder permissions?
P.S. I already have "permissions": ["nativeMessaging"] in my extension's manifest.json.
Also, since I enabled logging using the command:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-logging --v=1
I tailed the logs inside Library/Application Support/Google/Chrome/chrome_debug.log.
Here are the errors I got:
ERROR:native_process_launcher_posix.cc(98)] Error launching process
"Disconnected", source: chrome-extension://<extension_id>/background.js (6)
"Unchecked runtime.lastError: Failed to start native messaging host.", source: (0)
It's difficult to debug with this, error message is not clear and direct. Surprisingly also, I can't find a good comprehensive Chrome Native Messaging tutorials or examples.
Makes me wonder what is the standard way of communication between Chrome extension and Desktop apps, how do they send message to the app or even just invoke its method?
Any opinion would be greatly helpful.