简体   繁体   English

如何关闭除当前带有google chrome扩展名的所有标签

[英]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(){}) } }); }); 
What you do is loop through each tab and get i'ts id, then get the current tab id and check if the current tab id matches the tab id from the loop. 您要做的是遍历每个选项卡并获取ID,然后获取当前选项卡ID并检查当前选项卡ID是否与循环中的选项卡ID相匹配。 If they match, do nothing and if they don't match, remove the tab. 如果它们匹配,则什么也不做;如果它们不匹配,则删除该选项卡。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 chrome 扩展中的内容脚本按需关闭除活动选项卡之外的所有选项卡 - How to close all tabs except the active one on demand with content script in chrome extension 如何查看所有窗口中所有标签的URL? (谷歌浏览器扩展程序) - How to check URLs of all tabs in all windows? (Google Chrome Extension) 如何使用 Chrome 扩展程序关闭当前的 Chrome 选项卡? - How to close the current Chrome tab with a Chrome Extension? 通过javascript关闭所有浏览器标签(当前标签除外) - close all browser tabs except current tab via javascript chrome扩展程序关闭或更新标签 - chrome extension close or update tabs 如何为所有标签重复此代码? Chrome扩展程序 - How to repeat this code for all tabs? Chrome Extension 如何使用API​​关闭Google Chrome窗口中的所有标签 - How I can close all tabs in Google Chrome window using API Google Chrome扩展程序 - 同时为所有标签切换图标? - Google chrome extension - toggle icon for all tabs simultaneously? 如何关闭 iView 表中除当前行之外的所有展开行 - How to close all expanded rows except current one in iView Tables Chrome扩展程序:停止所有选项卡和窗口(活动的选项卡和窗口除外),以及播放音频时 - Chrome Extension: Stop all tabs and windows except for active one and if playing audio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM