2011-03-04 32 views
1

我不擅长JS,我有一些 - 我希望愚蠢的问题我没有看到我的代码...如果你们可以帮助我,我会真的很感激它。Chrome扩展问题得到标签页URL

我的扩展程序用当前选项卡的URL做了一些操作。它在我的背景页面上使用onUpdate事件工作正常,将选项卡的URL设置为一个变量,然后在弹出窗口中使用它。

事情是,如果用户启动,选择不同的选项卡,而不更新网址我的事件将不会再次触发...所以我现在还在听onSelectionChanged事件。

问题是onSelectionChanged事件的参数中没有“tab”对象,所以我不能要求tab.url属性。

我试图使用chrome.tabs.getCurrent()方法,但显然我做错了什么......我达到了我的极限 - 每一个小知识。

下面是代码,如果你们可以看看并指向正确的方向,我会非常感激。

<script> 
var tabURL = ''; 

var defaultURLRecognition = [ "test" ]; 

    // Called when the url of a tab changes. 
    function checkForValidUrl(tabId, changeInfo, tab) { 

      //THIS IS WHAT'S NOT WORKING, I SUPPOSE 
    if (tab==undefined) { 
     chrome.tabs.getCurrent(function(tabAux) { 
      test = tabAux; 
     }); 
    } 
      // 

    // If there's no URLRecognition value, I set the default one 
    if (localStorage["URLRecognition"]==undefined) { 
      localStorage["URLRecognition"] = defaultURLRecognition; 
     }; 
    // Look for URLRecognition value within the tab's URL 
    if (tab.url.indexOf(localStorage["URLRecognition"]) > -1) { 

    // ... show the page action. 
     chrome.pageAction.show(tabId); 
     tabURL = tab.url; 

    } 
    }; 

    // Listen for any changes to the URL of any tab. 
    chrome.tabs.onUpdated.addListener(checkForValidUrl); 
    // Listen for tab selection changes 
    chrome.tabs.onSelectionChanged.addListener(checkForValidUrl); 

</script> 

回答

3

我会做这样的事情:

function checkForValidUrl(tab) { 
    //... 
} 

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ 
    if(changeInfo.status == "loading") { 
     checkForValidUrl(tab); 
    } 
}); 

chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo){ 
    chrome.tabs.getSelected(null, function(tab){ 
     checkForValidUrl(tab); 
    }); 
}); 
+0

谢谢!它像一个魅力:) – frenetix 2011-03-09 14:09:30