How I can close all tabs in Google Chrome window using API

时光总嘲笑我的痴心妄想 提交于 2019-12-24 01:44:34

问题


I need close all tabs in Chrome window via my extension. What is the best practice now? How would you have done?


回答1:


In your background page, use chrome.tabs.query(...) to get all tabs, then call chrome.tabs.remove(...) to close that, code will look like:

chrome.tabs.query({}, function (tabs) {
    for (var i = 0; i < tabs.length; i++) {
        chrome.tabs.remove(tabs[i].id);
    }
});



回答2:


You can only close windows/tabs that you create yourself. That is, you cannot programmatically close a window/tab that the user creates.

For example, if you create a window with window.open() you can close it with window.close().

from https://stackoverflow.com/a/14373670/789377



来源:https://stackoverflow.com/questions/34897417/how-i-can-close-all-tabs-in-google-chrome-window-using-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!