[英]how to close all tabs except current with google chrome extension
I want to make the extension for google chrome to close all tabs except current. 我想对Google Chrome进行扩展,以关闭除当前以外的所有标签。 I have found examples to close all tabs, for instance: 我找到了关闭所有标签的示例,例如:
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.remove(tabs[i].id);
}
});
But I don't know how to add the condition for current tab in order to current tab stay opened. 但是我不知道如何为当前选项卡添加条件以使当前选项卡保持打开状态。 Could you give me example of code that closes all tabs except the current one? 您能给我一个示例代码,该代码关闭除当前选项卡以外的所有选项卡吗?
Thanks. 谢谢。
I had the same problem but then I came up with a way to do this. 我遇到了同样的问题,但是后来我想出了一种解决方法。 The code below is still a bit messy but works fine. 下面的代码仍然有点混乱,但是可以正常工作。
chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(d){document.getElementById("Current-tab-id").innerHTML = d[0].id;}) chrome.tabs.query({currentWindow: true}, function(tabs) { tabs.forEach(function(tab) { console.log(tab.id); var CurrentTabId = document.getElementById('Current-tab-id').innerHTML; console.log(CurrentTabId); if(CurrentTabId == tab.id) { } else { chrome.tabs.remove(tab.id, function callback(){}) } }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.