Firefox:关闭所有选项卡和*最后事件*

标签 firefox firefox-addon-webextensions

async function closeAllTabs(activeTab)
{
    const tabs = await browser.tabs.query({ currentWindow: true, active: false });
    for (const tab of tabs)
        browser.tabs.remove(tab.id);

    browser.tabs.remove(activeTab.id);  // Close the active tab last so that it would be first in the Recently Closed list.
}

browser.browserAction.onClicked.addListener(closeAllTabs);

如何确保事件选项卡最后关闭? 使用上面的代码,它会间歇性地工作。

最佳答案

使用 await 并一次性将所有 id 作为数组传递:

async function closeAllTabs(activeTab)
{
    const tabs = await browser.tabs.query({ currentWindow: true, active: false });
    await browser.tabs.remove(tabs.map(tab => tab.id));
    await browser.tabs.remove(activeTab.id);
}

关于Firefox:关闭所有选项卡和*最后事件*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62788634/

相关文章:

python - 从/usr/bin/* 执行命令时,来自 subprocess.run 的 FileNotFoundError - Python

javascript - 单击按钮时从内容脚本打开选项页面?

javascript - 是否可以在内容脚本中使用 onHistoryStateUpdated 事件?

html - 背景颜色和背景图像在 IE 和 Mozilla Firefox 中打印为 PDF 时不显示

java - Selenium 2.52.0 与 Firefox 51.0.1 兼容吗?

css - 纯 CSS : Responsive images breaking columns with border-box and padding on Firefox and Opera

javascript - 基于 "matches"在 content_scripts 中运行不同的脚本

css - 光标 : Invalid property value

jquery - CSS 无法在 Firefox 上加载,适用于 Chrome/Safari

javascript - 无法在 webextensions 中使用 AsmJS