Ivan
Ivan

Reputation: 41

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

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

Upvotes: 2

Views: 6465

Answers (2)

Haibara Ai
Haibara Ai

Reputation: 10897

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);
    }
});

Upvotes: 8

Maxali
Maxali

Reputation: 1962

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

Upvotes: -2

Related Questions