1

I am having some weird results with message passing. I have a content script interacting with a script embedded in my popup page. Currently, they are repeatedly sending the same messages and responses to eachother. I feel like this has something to do with my event listeners firing too much, or maybe the methods i've written to pass the messages are creating some sort of accidental message loop.

My popup script has some logic to inject my content script into every webpage the user visits:

document.addEventListener('DOMContentLoaded', function () { chrome.tabs.onUpdated.addListener(contextSwitch); }); //after navigating to a new url function contextSwitch() { var tabid = arguments[0]; var changeinfo = arguments[1]; var tab = arguments[2]; //keeps the script from being injected into a tab that's still loading. if(changeinfo.status === "complete"){ chrome.tabs.executeScript(tabid, {file : "inject.js"}, postInject); } else if (changeinfo.status ==="loading"){ console.log("loading fired"); } else{ console.log("Something went wrong."); console.log(changeinfo); } } //this fires after the script is injected. function postInject() { chrome.runtime.onConnect.addListener(function(port) { port.onMessage.addListener(messageBroker); port.postMessage({status:"connected"}); }); } //helps pick and choose what to do based on nthe message received function messageBroker() { var msg = arguments[0]; var sender = arguments[1]; if(msg.status == "initialized"){ sender.postMessage({status:"inject",contents:{notes:page.Notes}}); } } 

The content script opens up a port like this:

var extPort = chrome.runtime.connect({ name : "CS:" + window.location.href }); extPort.onMessage.addListener(function() { var msg = arguments[0]; var sender = arguments[1]; //the popup page sends the "connected" message after the port connects. if (msg.status === "connected") { var initmsg = { status : "initialized", contents : { url : window.location.href } }; sender.postMessage(initmsg); } if (msg.status === "inject") { var contents = msg.contents; if (contents) { for (item in contents) { //do stuff with the contents } } } sender.postMessage({ status : "received" }); }); 

Here is my manifest file:

{ "name": "name", "version": "0.0.1", "description": "chrome extension", "permissions": [ "activeTab", "tabs","<all_urls>","http://*/","https://*/" ], "browser_action": { "default_title": "ext", "default_icon": "icon.png", "default_popup": "popup.html" }, "background": { "scripts": ["chrome.js"], "persistent": false }, "manifest_version": 2, "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'" } 

My background script is embedded into my popup like this:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>popup</title> <meta name="description" content=""> <meta name="author" content="alex"> <script src="chrome.js"></script> </head> <body style="width:400px;height:400px;"> <div id=menu> Menu </div> <a href="#" id="button1">button1</a> <a href="#" id="button2">button2</a> </body> </html> 

My goal for this back and forth message passing is this:

  • inject the script into any appropriate page
  • the script tells the background that it's been injected and is ready to receive orders.
  • the background page gives it orders.
  • the content script performs some operation based upon those orders.
2
  • Have you by any chance mistaken a popup script for a background page ? Posting the manifest might help clear things up ! Commented Dec 12, 2013 at 18:45
  • I've posted my manifest, I think I meant to refer to it as a background script, woops! Commented Dec 12, 2013 at 20:58

1 Answer 1

1

If I understand correctly, the problem is that you have been mixing up the packground-page and popup code.

The popup page is executed every time you open the popup. This means every time the popup is shown, a couple of new listeners are registered (which continue to live on after the popup is closed). Thus you end up with a dozen identical listeners, responding to an event (which in turn results is moe and most importantly unexpected traffic).

I suggest you take a look at the core concepts of an extension, to help clear things up:

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I looked at an extensionizr template last night and realized what I had done wrong, I meant to post about it today.
Nice ! Come back with more questions, if you get stuck on a concept. BTW, if you find it appropriate feel free to also upvote this answer :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.