每个选项卡使用chrome.browserAction.setPopup [英] Using chrome.browserAction.setPopup per tab
问题描述
if(domains.contains(request.url)){
chrome.browserAction。 setPopup({
popup:tracking.html
});
} else {
chrome.browserAction.setPopup({
popup:nottracking.html
});
}
问题是,如果我切换选项卡,弹出窗口的内容保持标签之间相同。什么是正确的策略来处理这个问题?
- 以某种方式挂钩选项卡更改事件(如果存在这种可能性)?
- 将弹出内容的更改限制为当前选项卡? (我注意到chrome.browserAction.setPopup中有一个可选的
tabId
参数,但文档有点不足) -
所有帮助非常感谢!
解决方案选项1,绑定事件监听器:使用
chrome.tabs.onUpdated
监听URI更改,然后加上chrome.browserAction.setPopup
$ c> tabId 设置给定选项卡的弹出窗口。例如:
$ bchrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab) {
if(domains.contains(tab.url)){
chrome.browserAction.setPopup({
tabId:tabId,
popup:'tracking.html'
});
} else {
chrome.browserAction.setPopup({
tabId:tabId,
popup:'nottracking.html'
});
}
});
I'm writing a Chrome extension which dynamically changes the content of the popup window based on the current URL.
I'm doing something like this in background.js, which works fine:
if(domains.contains(request.url)){ chrome.browserAction.setPopup({ popup: "tracking.html" }); }else{ chrome.browserAction.setPopup({ popup: "nottracking.html" }); }
The problem is that if I switch tab, the content of the popup stays the same between tabs. What's the correct strategy to deal with this?
- Hook into the tab change event somehow (if such a possibility exists)?
- Limit the change of popup contents to the current tab? (I did notice that there's an optional
tabId
parameter for chrome.browserAction.setPopup, but the docs are a bit scant) - Something else?
All help very much appreciated!
解决方案Option 1, bind an event listener:
Use
chrome.tabs.onUpdated
to listen for URI changes, followed bychrome.browserAction.setPopup
with a giventabId
to set the popup for the given tab. For example:chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { if (domains.contains(tab.url)) { chrome.browserAction.setPopup({ tabId: tabId, popup: 'tracking.html' }); } else { chrome.browserAction.setPopup({ tabId: tabId, popup: 'nottracking.html' }); } });
这篇关于每个选项卡使用chrome.browserAction.setPopup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!