Chrome tab.create和tab.getSelected

时间:2011-04-28 09:20:11

标签: google-chrome google-chrome-extension

我有一些问题要将消息从后台页面传递到我的content_script.js。我希望有人可以指出我错在哪里。

background.html

//in a function
function myFunction() {
    chrome.tabs.create({"url":"myurl","selected":true},function(tab){
    updateTab(tab.id);
    });
}

//update Created new tab
function updateTab(tabId){
    chrome.tabs.getSelected(null, function(tab) {
        makeRequest(tab.id);  
    });}
//make request
function makeRequest(tabId) {
    chrome.tabs.sendRequest(tabId, {greeting: "hello"}, function(response) {
        console.log(response.farewell); 
    });
}

content_script.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  });

的manifest.json

"permissions": [
        "tabs","notifications","http://*/*"
    ],
    "content_scripts": [
    {
        "matches": ["http://*/*","https://*/*"],
        "js": ["content_script.js"]
    }],

我的问题是来自background.html的请求从未传递给content_script.js。我认为创建新选项卡和选择该选项卡的顺序一定存在一些问题,但我不知道如何解决这个问题。 感谢。

编辑: 到目前为止我已经做了。

background.html

chrome.browserAction.onClicked.addListener(function(tab) {
        var tabId = tab.id;
        chrome.tabs.getSelected(null, function(tab) {   
           validate(tab.url,tabId);
        });
    });
    function validate(url,tabId) {
        var filter = support(url);
        if(filter!=null) {
            getHTML(tabId,url,filter);
        }
        else{
            var notification = webkitNotifications.createHTMLNotification(
              'notification.html'  // html url - can be relative
            );
            notification.show();
            setTimeout(function(){
                notification.cancel();
            }, 10000);  //10 sec
        }
    }
function getHTML(tabId,url,filter) {
        console.log("request"); 
            chrome.tabs.sendRequest(tabId, {action:"getHTML"}, function(response) {     
            var html = response.html;
            console.log(html);
            var taburl = ("some thing on server");  
            chrome.tabs.create({"url":taburl,"selected":true}, function(tab){
                var tabId = tab.id;
                chrome.tabs.onUpdated.addListener(function(tabId, changeInfo){
                    if(changeInfo.status == "loading"){
                            console.log("loading");
                  }
                  if(changeInfo.status == "complete"){
                          chrome.tabs.onUpdated.    removeListene(arguments.callee);                    
                        updateTab(tabId,url,filter,html);

                  }
                });
              }); 
            });
    }
function updateTab(tabId,url,filter,html) {
        chrome.tabs.sendRequest(tabId, {action:"updateTab"}, function(response) {
                //submit form
            chrome.tabs.executeScript(tabId, {code: 'document.getElementById(\"hiddenbutton\").click();'});
        });
    }

content_script.js

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    var action = request.action;
    console.log(action);
    if(action == "getHTML") {
        var html = document.body.innerHTML;
        console.log(html);
        sendResponse({html:document.body.innerHTML});
    }
    else{
    //do update on page from server
    sendResponse({});
    }
});

它按照我的预期工作,但仍然有一些我不理解的点,特别是删除了监听器 chrome.tabs.onUpdated.removeListene(arguments.callee); 。如果有任何问题,我希望有人有机会看看并纠正我。感谢。

1 个答案:

答案 0 :(得分:7)

background.html可以简化为:

//in a function
function myFunction() {
    chrome.tabs.create({"url":"myurl","selected":true}, function(tab){
        makeRequest(tab.id);
    });
}

//make request
function makeRequest(tabId) {
    chrome.tabs.sendRequest(tabId, {greeting: "hello"}, function(response) {
        console.log(response.farewell); 
    });
}

如果它仍然无法正常工作,那么可能是因为选项卡尚未完成加载(在chrome.tabs.create回调中记录tab.status以检查是否为真)。有两种解决方案,或者您在为此标签ID进行过滤时为chrome.tabs.onUpdated添加了一个监听器,或者您使用tab send the request而不是background.html。