I need close all tabs in Chrome window via my extension. What is the best practice now? How would you have done?
Asked
Active
Viewed 5,995 times
2
-
Possible duplicate of [Close Current Tab](http://stackoverflow.com/questions/14373625/close-current-tab) – Idos Jan 20 '16 at 10:28
-
I suggest not writing malicious software. Malicious software is software that goes beyond the normal scope of an application to control or alter the state of the computer (and its applications) than the intended scope of the software you are developing. This extension you want to write is doing something that goes outside the limited range of the software. Closing windows that do not belong to you. You wouldn't go to your neighbors house and open the door without knocking, and this conceptually aligns with how you should write code. – zackery.fix Jan 20 '16 at 10:31
-
Incorrect duplicate; Chrome extension would use `chrome.tabs` API. However, this question is too broad. – Xan Jan 20 '16 at 11:25
-
Also; Chrome windows can't exist without a tab. If you close all tabs, the window will close as well. If that's what you want, you might as well close windows, not tabs. If that's not what you want, you certainly didn't make it clear. – Xan Jan 20 '16 at 11:26
-
If you really need to close Google Chrome windows. (for your personal reason, not for creating malware), you can write your own API into chromium core and build your own browser. – Sungguk Lim Jan 20 '16 at 11:32
2 Answers
8
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);
}
});

Haibara Ai
- 10,703
- 2
- 31
- 47
-
1Correct code, but the permission is not needed for any of this. Permission is only needed to access details of the tab. Querying / removing is not restricted. – Xan Jan 21 '16 at 10:33
-
I am getting this error: Cannot read property 'query' of undefined at
:1:13 – Jay Momaya Mar 16 '20 at 11:59
-1
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().
-
1Exactly. The OP sounds like he wants to write a malicious code extension. – zackery.fix Jan 20 '16 at 10:33
-
_“You can only close windows/tabs that you create yourself”_ – that is for JS running in the context of a website though. From within an extension, things might be different. – CBroe Jan 20 '16 at 10:38
-
@zackery.fix I want close all tabs, because all tabs has been created by my extension.I am also satisfied with the answer, how do I close only my tabs via Chrome API. – Ivan Jan 20 '16 at 10:39
-
Then hold a collection of the windows you need to close, then iterate over them when you want to close them all. Like think about if the user has Pandora open, and you go off and close all the tabs, this would frustrate the user considering the tab with Pandora has nothing to do with your extension. – zackery.fix Jan 20 '16 at 10:42
-
You have to expect the unexpected when dealing with the user experience. What makes you expect that the user hasn't opened their own tab? – zackery.fix Jan 20 '16 at 10:44
-
-