2011-06-03 52 views

回答

1

在你的背景页面只需注册标签更新通知:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 
{ 
    if (changeInfo.status == "loading") 
    { 
     var url = tab.url; 
     var iconPath = ??? 
     chrome.pageAction.setIcon({tabId: tabId, path: iconPath}); 
    } 
}); 

该处理器将被称为每当一个标签改变位置。您无需关心当前选择哪个选项卡,因为您将为每个选项卡定义不同的图标。不过,如果你想这样做 - http://code.google.com/chrome/extensions/tabs.html#event-onSelectionChanged是要走的路。

2

我想我已经想出了这一个。你需要两个听众。一个用于检测标签何时被更改,另一个用于检测标签何时被更新。然后他们都可以触发同样的功能。这里是什么在后台文件...

function changeIcon() { 
    //query the information on the active tab 
    chrome.tabs.query({active: true}, function(tab){ 
     //pull the url from that information 
     var url=tab[0].url; 
     //do whatever you need to do with the URL 
     //alert(url); 
     //change the icon 
     chrome.browserAction.setIcon({path: 'pathToIcon'}); 
    }); 
} 

//listen for new tab to be activated 
chrome.tabs.onActivated.addListener(function(activeInfo) { 
    changeIcon(); 
}); 

//listen for current tab to be changed 
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
    changeIcon(); 
}); 
相关问题