Reputation: 6193
I'm trying to create a Hello World app for Chrome Native Messaging. I've created a Chrome extension, a binary executable and html page. I'm on Linux and using Chromium.
I think I've registered everything properly, however, it still isn't working.
From my web page I send a message to my extension, and here's a part of code of my extention:
chrome.runtime.onMessage.addListener(function (msg, snd, sndResp) {
var prt = chrome.runtime.connectNative('com.example.my_app');
prt.postMessage(msg); // Error in event handler for runtime.onMessage: Error: Attempting to use a disconnected port
Note that the "msg" isn't empty and has the same content in it as one that I put into it on the html page. So there's no issue about that.
However, in the extension the error is:
Error in event handler for runtime.onMessage: Error: Attempting to use a disconnected port
Upvotes: 2
Views: 2360
Reputation: 4950
The error may caused when a connection get closed. It may failed because the tab is no longer active to receive the message.
Try to use runtime.connect
or tabs.connect
respectively. It allows you to distinguish between different types of connections.
When establishing a connection, each end is given a runtime.Port
object which is used for sending and receiving messages through that connection.
Here is how you open a channel from a content script, and send and listen for messages:
var port = chrome.runtime.connect({name: "knockknock"});
port.postMessage({joke: "Knock knock"});
port.onMessage.addListener(function(msg) {
if (msg.question == "Who's there?")
port.postMessage({answer: "Madame"});
else if (msg.question == "Madame who?")
port.postMessage({answer: "Madame... Bovary"});
});
Upvotes: 1