Google chrome Chrome扩展:如何更改选项卡焦点上的图标?
我想能够改变我的扩展图标,根据我目前浏览的网站。如何侦听选项卡焦点的更改?只需在后台页面中注册选项卡更新通知:Google chrome Chrome扩展:如何更改选项卡焦点上的图标?,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,我想能够改变我的扩展图标,根据我目前浏览的网站。如何侦听选项卡焦点的更改?只需在后台页面中注册选项卡更新通知: chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if (changeInfo.status == "loading") { var url = tab.url; var iconPath = ??? chrome.pageAction
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});
}
});
每当选项卡更改位置时,将调用此处理程序。您不必关心当前选择了哪个选项卡,因为您将为每个选项卡定义不同的图标。不过,如果你想这样做的话,这是一条路。我想我已经想出了这个办法。你需要两个听众。一个用于检测选项卡何时更改,另一个用于检测选项卡何时更新。然后它们都可以触发相同的函数。下面是背景文件中的内容
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();
});