Chrome development - chrome.tabs.sendMessage does not notify runtime

I am trying to send a message to a newly created / updated tab and get it there:

var tabAction = 'create';      // tabAction equals *create* or *update*

chrome.tabs[tabAction]({
    url:  chrome.extension.getURL('/somepage.htm'),
    active: true
}, function(_tab) {
    chrome.tabs.sendMessage(_tab.id, {
        message: 'some custom message',
        arg: 'some arg'
    });
});

After this call, one of the scripts (included in the title of the open page) should receive this message and perform the following actions:

(function(window, document, jQuery) {
    "use strict";


    chrome.runtime.onMessage.addListener(function(message) {
        // Do stuff
    });
})(window, document, jQuery);

Now my problem is:

If tabAction is set to "create", everything works fine - the page loads, the main script sends a message, adds an extension debugger: "tabsendMessage is called" and "runtime.onMessage is notified" and the script page does what it should.

tabAction "" - , script , ; " tabs.sendMessage".

? .

+4
2

, chrome.tabs.create chrome.tabs.update.

chrome.tabs.sendMessage, ( " . ". chrome.runtime.lastError.message).

- chrome.tabs.onUpdated, , :

chrome.tabs.update({
    url: chrome.runtime.getURL('/somepage.htm')
}, function(tab) {
    chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo) {
        if (tabId === tab.id && changeInfo.status == 'complete') {
            chrome.tabs.onUpdated.removeListener(listener);
            // Now the tab is ready!
            chrome.tabs.sendMessage(tabId, 'custom message whatever');
        }
    });
});

chrome.tabs.create - crbug.com/411225. , :

var tabAction = 'create'; // Or update.
chrome.tabs[tabAction]({
    url: chrome.runtime.getURL('/somepage.htm')
}, function(tab) {
    // Called when the tab is ready.
    var onready = function() {
        onready = function() {}; // Run once.
        chrome.tabs.onUpdated.removeListener(listener);
        // Now the tab is ready!
        chrome.tabs.sendMessage(tab.id, 'custom message whatever');
    };

    // Detect update
    chrome.tabs.onUpdated.addListener(listener);

    // Detect create (until crbug.com/411225 is fixed).
    chrome.tabs.get(tab.id, function(tab) {
        if (tab.status === 'complete') {
            onready();
        }
    });

    function listener(tabId, changeInfo) {
        if (tabId === tab.id && changeInfo.status == 'complete') {
            onready();
        }
    }
});
+5

chrome.tabs.sendMessage chrome, , manifest.json. , "match": [ "http:///" ] , .

"background": {
  "scripts": [ "background.js" ],
  "persistent": false
},
"content_scripts": [ {
"js": [ "content.js" ],
"all_frames": true,
"matches": [ "http://*/*"]
}],
0

All Articles