簡體   English   中英

如何關閉除當前帶有google chrome擴展名的所有標簽

[英]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(){}) } }); }); 
您要做的是遍歷每個選項卡並獲取ID,然后獲取當前選項卡ID並檢查當前選項卡ID是否與循環中的選項卡ID相匹配。 如果它們匹配,則什么也不做;如果它們不匹配,則刪除該選項卡。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM