Cyrus Roshan
Cyrus Roshan

Reputation: 202

Chrome Message passing. Uncaught Error: Error connecting to extension

So I'm very new to Chrome's message passing, and I'm trying to use it to have my background page be alerted when the DOM of a page is modified (using some injected js).

I'm only trying to have one way communication, but if I open up console on the tab while the extension is running, it tells me "Uncaught Error: Error connecting to extension [my extension id]."

For simplicity, I'll say my background page's background.js has these lines of code:

chrome.runtime.onMessage.addListener(
  function(){
    if (modified == "true") {
      alert("modified message recieved");
      fourth();
    }
  }
);

And my content script is:

function subtreeModified(){
  chrome.runtime.sendMessage({modified: "true"});
  alert("modified message sent");
}

document.addEventListener("DOMSubtreeModified", subtreeModified, false);

I've been testing this, and whenever the page DOM is modified, I get a bunch of alerts that say "modified message sent," but none that say "modified message recieved." Thoughts/tips?

Upvotes: 0

Views: 1454

Answers (1)

jfriend00
jfriend00

Reputation: 708026

You aren't declaring and using the arguments sent to the receiver's event listener. Your modified argument is sent as a property on an argument passed to your callback. That's where you have to get it from when using your code. Also, I added a console.log() at the start of your callback so you can see if it's getting called:

chrome.runtime.onMessage.addListener(function(message) {
    console.log("received message");
    if (message.modified == "true") {
        alert("modified message recieved");
        fourth();
    }
});

Note: it's a little odd to be passing strings for true and false. Why not just use booleans?

Upvotes: 1

Related Questions