谷歌chrome sendResponse无法在chrome.tabs.query回调

时间:2016-02-22 17:20:20

标签: google-chrome google-chrome-extension

我是Chrome扩展程序的新用户,我发现了一个非常奇怪的事情,任何人都可以解释,请提前感谢。

background.js

 chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.type == "tab_start"){
            //this works fine since
            sendResponse({tab_id: sender.tab.id}); 

            //this does not work, it seams the response never gets sent out
            //I know this is kind of silly, we can get sender tab ID like above         
           //in the beginning, I did noticed I can do it the right way above
           //but anyway, I found this does not work in the query callback by accident
           //can you explain for me?

            chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
                sendResponse({tab_id:tabs[0].id});
                consloe.log(tabs[0].id);
            });

        } else {
            sendResponse();
        }

        chrome.pageAction.show(sender.tab.id);

    });

注射联系人JS:

var interval = 10;
var myTabId = 0; 
//the callback never gets called, since the message never gets sent
//but why???
chrome.runtime.sendMessage({type: "tab_start"}, function(response) {
    console.log(response);
    myTabId = response.tab_id;
    var readyStateCheckInterval = setInterval(function() {
        if (document.readyState === "complete") {
            clearInterval(readyStateCheckInterval);
            // ----------------------------------------------------------
            // This part of the script triggers when page is done loading
            // ----------------------------------------------------------
        }
    }, interval);
});

再次感谢。

0 个答案:

没有答案