Chrome扩展程序 - 查找所有打开的选项卡并执行全部脚本 [英] Chrome Extension - Find all open tabs and execute script on all
本文介绍了Chrome扩展程序 - 查找所有打开的选项卡并执行全部脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试修改我的代码以查看点击扩展按钮的时间,它将在所有打开的选项卡上执行,而不仅仅是活动的选项卡。
Trying to modify my code for when the extension button is clicked, it will execute on all open tabs instead of only the active one.
背景。 js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, { file: "jquery-2.1.0.min.js" }, function() {
chrome.tabs.executeScript(null, {file: "change.js"});
});
});
manifest.json
{
"manifest_version": 2,
"name": "GSHOP",
"version": "2",
"description": "I do Stuff",
"background": {
"persistent": false,
"scripts": ["jquery-2.1.0.min.js", "background.js"]
},
"browser_action": {
"name": "Manipulate DOM",
"icons": ["icon.png"],
"default_icon": "icon.png"
},
"permissions": [
"activeTab",
"tabs",
"http://*/*", "https://*/*"
]
}
我相信我有逻辑,我无法想象如何去做。我相信我需要找到有多少个标签打开 tabs.length?
并迭代它们,但我无法让它工作。
I believe I have the logic down I just can't figure how to do it. I believe I need to find how many tabs are open tabs.length?
and iterate over them, but I just cannot get it to work.
不起作用
Doesn't Work
chrome.browserAction.onClicked.addListener(function(tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.executeScript(tabs[i].id, {file: "jquery-2.1.0.min.js" }, function() {
chrome.tabs.executeScript(tabs[i].id, {file: "change.js"});
});
}
});
推荐答案
试试像这样:
Try like this:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.query( {} ,function (tabs) { // The Query {} was missing here
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.executeScript(tabs[i].id, {file: "jquery-2.1.0.min.js"});
chrome.tabs.executeScript(tabs[i].id, {file: "change.js"});
}
});
});
这篇关于Chrome扩展程序 - 查找所有打开的选项卡并执行全部脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文