Russell C.
Russell C.

Reputation: 608

Is it possible for a Chrome extension to close a current tab?

Is it possible for a Chrome extension to close a current tab that the user has opened? If so, how would I do this?

Upvotes: 2

Views: 3163

Answers (1)

Makyen
Makyen

Reputation: 33376

Yes, it is, of course, possible for a Chrome extension to close a currently open tab. The API is chrome.tabs.remove().

You must supply the tab ID, or array of tab IDs, which you desire to close/remove as the first parameter. The second, optional, parameter is a callback function which is called once the tabs are removed.

If you desire to close the active tab in the current window, you may need to obtain that information first. You can do so with chrome.tabs.query(). The code would look like:

chrome.tabs.query({active:true,currentWindow:true},function(tabs){
    //'tabs' will be an array with only one element: an Object describing the active tab
    //  in the current window. To remove the tab, pass the ID: to chrome.tabs.remove().
    chrome.tabs.remove(tabs[0].id);
});

Upvotes: 4

Related Questions