最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome development - chrome.tabs.sendMessage not notifying runtime - Stack Overflow

programmeradmin0浏览0评论

I am trying to send a message to newly created / updated tab and receive 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 header of the opened page) has to receive this message and do further actions:

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


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

Now my problem:

If tabAction is set to "create" everything works fine - the page is being loaded, the main script sends the message, the extension debugger shows: "Invoked tabs.sendMessage" and "Notified of runtime.onMessage" and the page script does what it has to.

If tabAction is set to "update" - the page is being redirected properly and the main script sends the message as well, but the message is not being sent to the runtime; the debugger just stops at "Invoked tabs.sendMessage".

Why this odd behavior? Thanks for all the further replies.

I am trying to send a message to newly created / updated tab and receive 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 header of the opened page) has to receive this message and do further actions:

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


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

Now my problem:

If tabAction is set to "create" everything works fine - the page is being loaded, the main script sends the message, the extension debugger shows: "Invoked tabs.sendMessage" and "Notified of runtime.onMessage" and the page script does what it has to.

If tabAction is set to "update" - the page is being redirected properly and the main script sends the message as well, but the message is not being sent to the runtime; the debugger just stops at "Invoked tabs.sendMessage".

Why this odd behavior? Thanks for all the further replies.

Share Improve this question edited Dec 10, 2014 at 17:47 donnikitos asked Dec 10, 2014 at 16:11 donnikitosdonnikitos 9861 gold badge13 silver badges20 bronze badges 2
  • Where are these two lines "Notified of runtime.onMessage" and "Invoked tabs.sendMessage"? I can't see any debug message like those. Include more relevant code in your question, please. – Marco Bonelli Commented Dec 10, 2014 at 16:47
  • These lines are shown in the "Chrome App and Extension Developer Tool" under the realtime behavior tab – donnikitos Commented Dec 10, 2014 at 17:17
Add a ment  | 

2 Answers 2

Reset to default 8

There is no guarantee that the page has pletely loaded when the callback of chrome.tabs.create or chrome.tabs.update is invoked.

If the page does not finish loading when you call chrome.tabs.sendMessage, then the message is not going to be received by the page (you might see "Could not establish connection. Receiving end does not exist." if you check chrome.runtime.lastError.message).

The correct way to solve the problem is to use chrome.tabs.onUpdated to detect when the tab has finished loading:

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 == 'plete') {
            chrome.tabs.onUpdated.removeListener(listener);
            // Now the tab is ready!
            chrome.tabs.sendMessage(tabId, 'custom message whatever');
        }
    });
});

This does not work for chrome.tabs.create at the moment because of crbug./411225. Until that bug is fixed, you have to use the following:

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./411225 is fixed).
    chrome.tabs.get(tab.id, function(tab) {
        if (tab.status === 'plete') {
            onready();
        }
    });

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

I had a similar problem with chrome.tabs.sendMessage in my chrome extension I solved it using the following snippet in manifest.json. Note here the most important things is "matches": [ "http:///"] in the manifest for content scripts , as testing the code with and without it yields success and error scenarios respectively .

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

评论列表(0)

  1. 暂无评论