A simple messaging error in Google Chrome

I am trying to transfer a message between my popup to a background image using a single message interface (for this I use code similar to the one shown in the Google example).

pop-up window:

chrome.extension.sendMessage( {msg: "this is popup msg"}, function(b){ alert('this is popups callback func' +b.backgroundMsg); }); 

and so I listen (and answer) in background.js:

 chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) { sendResponse({backgroundMsg: "this is background msg"}); }); 

Now, when I check everything on the console, the messages exchange normally, but I get the following error:

 Error in event handler for 'undefined': Cannot call method 'disconnect' of null TypeError: Cannot call method 'disconnect' of null at chromeHidden.Port.sendMessageImpl (miscellaneous_bindings:285:14) at chrome.Event.dispatch (event_bindings:237:41) at Object.chromeHidden.Port.dispatchOnMessage (miscellaneous_bindings:250:22) 

Any thoughts?

+4
source share
1 answer

Copying the sample code and filling in the gaps with the manifest and pop-up structure led to the creation of a fully working extension without errors. Not sure why you get an error message. Try my code and see if you can get rid of the error.

Tips

  • Alerts in the pop-up window will cause a warning to appear on the screen, and then both will be closed before you can see them. It is probably best to simply log into the console for testing as follows.

Example

manifest.json

 { "name": "Stackoverflow Message Passing Example", "manifest_version": 2, "version": "0.1", "description": "Simple popup with message passing for Stackoverflow question", "browser_action": { "default_popup": "popup.html" }, "background": { "scripts": ["background.js"] } } 

background.js

 chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { sendResponse({backgroundMsg: "this is background msg"}); }); 

popup.html

 <html> <head> <script src="popup.js"></script> <style type="text/css" media="screen"> body { width: 100px; height: 100px; } </style> </head> <body> <p>Check the console</p> </body> </html> 

popup.js

 chrome.runtime.sendMessage( {msg: "this is popup msg"}, function(b){ console.log('this is popups callback func ' + b.backgroundMsg); }); 

Screenshot example

Pressing an extension button with a browser open on exampley.com

Clicking extension button with browser window opened to exampley.com

Popup Extension Console Log

Console log of extension popup

Here are the zip files that I used http://mikegrace.s3.amazonaws.com/stackoverflow/simple-popup.zip

+9
source

All Articles