Chrome 扩展程序可循环关闭未选定的选项卡
我为 Safari 制作了这个扩展,可以关闭当前页面上的非活动选项卡,
(var tabss = safari.application.activeBrowserWindow.tabs;
for (n=0; n<tabss.length; n++)
{
if(tabss[n] != safari.application.activeBrowserWindow.activeTab)
tabss[n].close();
}
)
我想为 Chrome 制作相同的扩展。但 Chrome 有不同的做事方式。我仍然想在选项卡索引上运行循环,如果它们不是选定的选项卡,则关闭它们。我已经能够获取 Windows 索引的长度,但我不知道如何多次执行关闭选项卡循环以确保不关闭所选选项卡。我已经能够通过这样做获得长度:
<html>
<head>
<script>
var targetWindow = null;
var tabCount = 0;
function start(tab) {
chrome.windows.getCurrent(getWindows);
}
function getWindows(win) {
targetWindow = win;
chrome.tabs.getAllInWindow(targetWindow.id, getTabs);
}
function getTabs(tabs) {
tabCount = tabs.length;
alert(tabCount);
}
// Set up a click handler so that we can merge all the windows.
chrome.browserAction.onClicked.addListener(start);
</script>
</head>
</html>
从 http://code.google 收集.com/chrome/extensions/samples.html 合并窗口。
现在我想运行循环,但我不知道如何告诉循环不要关闭选定的选项卡。我正在考虑让循环将循环选项卡与所选窗口的选项卡 ID 进行比较,并且它不会关闭它并移动到循环中的下一个选项卡索引号。
类似这样的:
(
for (n=0; n<tabCount; n++)
{
if(chrome.tabs[n].id != tab.id)
chrome.tabs[n].remove();
}
)
但我不知道如何注入当前的 tabid,因为所有回调函数都被这个 javascript hack/noob 难住了。根据我的理解,我无法从另一个函数引入变量。
I've made this extension for Safari that closes inactive tabs on the current page
(var tabss = safari.application.activeBrowserWindow.tabs;
for (n=0; n<tabss.length; n++)
{
if(tabss[n] != safari.application.activeBrowserWindow.activeTab)
tabss[n].close();
}
)
I want to make the same for Chrome. But Chrome has a different way of doing things. I still want to run the loop on the index of tabs and close them if they aren't the selected tab. I've been able to get the length of the windows index but I don't know how to do a close tab loop that many times that will make sure not to close the selected tab. I have been able to get the length by doing this:
<html>
<head>
<script>
var targetWindow = null;
var tabCount = 0;
function start(tab) {
chrome.windows.getCurrent(getWindows);
}
function getWindows(win) {
targetWindow = win;
chrome.tabs.getAllInWindow(targetWindow.id, getTabs);
}
function getTabs(tabs) {
tabCount = tabs.length;
alert(tabCount);
}
// Set up a click handler so that we can merge all the windows.
chrome.browserAction.onClicked.addListener(start);
</script>
</head>
</html>
Gleaned from http://code.google.com/chrome/extensions/samples.html Merge Windows.
Now I want to run the loop but I can't figure out how to tell the loop not to close the selected tab. I was thinking of having the loop compare the looped tab to the tab ID of the selected window and it won't close that and move to the next tab index number in the loop.
Something like:
(
for (n=0; n<tabCount; n++)
{
if(chrome.tabs[n].id != tab.id)
chrome.tabs[n].remove();
}
)
But I don't know how to inject the current tabid as all the callback functions has this javascript hack/noob stumped. I can't introduce a variable from another function from what I understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到:
This should do it: