您可以使用后台脚本来实现此目的:
chrome.browserAction.setIcon({path: '../images/1.png', tabId: info.tabId});
您可以与一些侦听器一起调用它:
chrome.tabs.onActivated.addListener()
chrome.tabs.onUpdated.addListener()
chrome.runtime.onMessage.addListener()
示例 1:让我向您展示如何通过向后台脚本发送消息来从内容脚本触发它:
在内容脚本中,发送消息:
chrome.runtime.sendMessage({icon1: true})
在后台脚本中:
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse)
{
if(msg.icon1) {
chrome.tabs.query({active:true, windowType:"normal", currentWindow: true},function(d){
var tabId = d[0].id;
chrome.browserAction.setIcon({path: '../icon1.png', tabId: tabId});
})
}
}
示例2:
chrome.tabs.onActivated.addListener(function(info){
chrome.tabs.get(info.tabId, function(change){
var matching = false; // functionality to determine true/false
if(matching) {
chrome.browserAction.setIcon({path: '../icon1.png', tabId: info.tabId});
return;
} else {
chrome.browserAction.setIcon({path: '../icon2.png', tabId: info.tabId});
}
});
});