1

Recently, Google Chrome is updated to new version 49.0.2623.87. After this update, my extension stopped working. (Previous versions works fine)

Here is the part of my code

// Content.js
chrome.runtime.onMessage.addListener(function (message, sender, response) {
    console.log(message); // > { action: "start" } - I can see message what i've sent.
    if(message.action == "start"){
        response({result: true});
    }else {
        response({result: false});
    }
});

// Background.js
chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
    console.log(tabs[0]); // here is tab { object } what I've expected
    console.log(tabs[0].id);   // here id for example 19

    // Background.js
    chrome.tabs.sendMessage(tabs[0].id, {action: "start"}, function(response){
        // RESPONSE = undefined
    });
});

Probably something changed in Chrome Extension Api?

I tried to research it, but not found for now. I'll keep looking.. but hope that somebody knows about it!

Thanks!

Also, I found this bug in Chrome bug-tracker.

aClever
  • 11
  • 4
  • Can't tell without knowing how your content script is injected and how you're getting `TAB_ID` – Xan Mar 13 '16 at 13:08
  • How are the content scripts injected? Through the manifest? When does this `query` execute? Do you see the content script receiving the message (logging)? – Xan Mar 13 '16 at 14:12
  • 1
    Manifest: `"content_scripts": [ { "matches": [ "" ], "js": [ "content.js" ] } ]` `"background": { "scripts": [ "background.js" ] },` Query for Background Script is executed on click on extension icon (in the extensions panel). – aClever Mar 13 '16 at 14:17
  • Unlikely that anything changed. Could it be [this](http://stackoverflow.com/questions/23895377/sending-message-from-a-background-script-to-a-content-script-then-to-a-injected/23895822#23895822)? – Xan Mar 13 '16 at 14:19
  • Not really. My problem is that now when I send a request from background to content it comes back empty, hence response=undefined. The reason I suspect something ha changed is because it's been working like this for several months and we've seen this change today after upgrading Chrome to the latest version – aClever Mar 13 '16 at 14:30
  • My point is, maybe you're sending while the content script is not ready (not yet injected). – Xan Mar 13 '16 at 14:30
  • Content.js: `chrome.runtime.onMessage.addListener(function (message, sender, response) { console.log(message); // > { action: "start" }; I see message that i've sent from background.js. // but this response I can't see at background response({result: true}) });` – aClever Mar 13 '16 at 14:37
  • 1
    Then I can only answer with a shrug and "should work". Maybe an [MCVE](http://stackoverflow.com/help/mcve) would help. – Xan Mar 13 '16 at 14:38
  • I agree. But what drives me nuts is that it used to work before the update. Thanks anyway – aClever Mar 13 '16 at 14:40
  • I'm having a very similar problem. The difference is that my listener is on background.js. – dfranca Mar 16 '16 at 10:44

1 Answers1

-1

As [1] says about sendResponse:

This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

[1]: https://developer.chrome.com/extensions/runtime#event-onMessage

Even though I called sendResponse synchronously I still had to return true to get rid of this error.

ilyaigpetrov
  • 3,657
  • 3
  • 30
  • 46