Javascript Chrome-Extension:遍历所有标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5409242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome-Extension: iterate through all tabs?
提问by Skizit
How would I iterate through all tabs a user has open and then check if they have a particular HTML item with id = 'item'
?
我将如何遍历用户打开的所有选项卡,然后检查他们是否有特定的 HTML 项目id = 'item'
?
采纳答案by Sindar
You can make it like this :
你可以这样做:
chrome.tabs.getAllInWindow(null, function(tabs){
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.sendRequest(tabs[i].id, { action: "xxx" });
}
});
After that to look after your item, if you can make it like this :
在那之后照顾你的物品,如果你能做到这样:
document.getElementById('item')
Don't forget that you can't manipulate the HTML by using the "background page" So the first code snip is for the background page, and the second have to be on a content script ;)
不要忘记你不能通过使用“背景页面”来操作 HTML 所以第一个代码片段是用于背景页面,第二个代码必须在内容脚本上;)
回答by Golden Flying Broom
It appears this method has been deprecated in favor of chrome.tabs.query
:
似乎此方法已被弃用,以支持chrome.tabs.query
:
http://developer.chrome.com/extensions/tabs.html#method-query
http://developer.chrome.com/extensions/tabs.html#method-query
So now you'd want to do:
所以现在你想做:
chrome.tabs.query({}, function(tabs) { /* blah */ } );
Passing an empty queryInfo
parameter would return all of the tabs.
传递空queryInfo
参数将返回所有选项卡。
回答by Emeeus
This is a not deprecated vanilla way (may 2019):
这是一种未弃用的香草方式(2019 年 5 月):
chrome.tabs.query({}, function(tabs){
tabs.forEach(tb => {
chrome.tabs.sendMessage(tb.id, { action: "xxx" });
});
});
回答by Aleksandr Golovatyi
I use this one
我用这个
chrome.tabs.getAllInWindow(null, function(tabs) {
$.each(tabs, function() {
// u can use 'this.id' to work with evey tab
});
});