My chrome extension uses long-lived 'Port' object for message passing between 'content script' and 'popup' page. The 'popup' is able to send a message to the 'content script' event listener. But, the 'Port' object in the 'content script' is unable to send message to the 'popup' page.
var port = chrome.extension.connect({"name":"swap"});
// listener for incoming connections
chrome.extension.onConnect.addListener(function( incomingPort ){
// listener on incoming messages
incomingPort.onMessage.addListener(function( msg ){
if( msg.command === 'get_scripts' ){
//do work
}
var scrs = { 'scripts' : 'name' };
var result = port.postMessage( scrs );
});
});
When executing 'port.postMessage(Object obj)' , the plugin throws the following Error,
Error in event handler for 'undefined': Attempting to use a disconnected port object Error: Attempting to use a disconnected port object
at PortImpl.postMessage (miscellaneous_bindings:54:5)
at chrome-extension://loiamkgdhfjdlkcpehnebipeinpcicfj/swap.js:27:31
at [object Object].dispatch (event_bindings:203:41)
at Object.<anonymous> (miscellaneous_bindings:250:22) event_bindings:207
I have tried using 'Port' object and 'incomingPort' object, both throw the same 'Error'. It feels like it has to do with the scope of the pre-created 'Port' object.
The plugin code is available at this git repository https://github.com/snambi/chrome_plugin/tree/master/src/chrome
What is wrong in this plugin?