[英]How to close all tabs except the active one on demand with content script in chrome extension
[英]how to close all tabs except current with google chrome extension
我想對Google Chrome進行擴展,以關閉除當前以外的所有標簽。 我找到了關閉所有標簽的示例,例如:
chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) {
chrome.tabs.remove(tabs[i].id);
}
});
但是我不知道如何為當前選項卡添加條件以使當前選項卡保持打開狀態。 您能給我一個示例代碼,該代碼關閉除當前選項卡以外的所有選項卡嗎?
謝謝。
我遇到了同樣的問題,但是后來我想出了一種解決方法。 下面的代碼仍然有點混亂,但是可以正常工作。
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.