Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Tab in Chrome Extension

I have a function called in popup.html that creates a tab, inserts a mailto to trigger a local (or gmail) mail event. It's my desire for it to then close itself. I've tried numerous things, but it seems like I need something that does the equivalent of:

tabId = chrome.tabs.query(I DON'T KNOW!);
chrome.tabs.remove(tabId);

here's the current code:

var query = { active: true, currentWindow: true };
function callback(tabs) {
    var currentTab = tabs[0];
    console.log(currentTab);
}
chrome.tabs.remove(chrome.tabs.query(query, callback));

but it's not working.

if useful, here's how I create the tab (which does work as desired):

    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
        getTabs(tabs, function(full_mail_link){
          chrome.tabs.create({ url: full_mail_link });
        });
    });

any help would be greatly appreciated!

like image 916
Jeremy Toeman Avatar asked Sep 05 '25 01:09

Jeremy Toeman


2 Answers

I don't know what your getTabs function does. Yet if you know how to find the tab id of the tab you want all you need to do is

chrome.tabs.remove(tabId, optionalCallback);
like image 138
Edwin Reynoso Avatar answered Sep 07 '25 02:09

Edwin Reynoso


this must be work:

chrome.tabs.getSelected(null, function(tab) {
 chrome.tabs.remove(tab.id);
 });
like image 30
AmineBmg Avatar answered Sep 07 '25 01:09

AmineBmg