Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open tab, wait for it to finish loading then close it

I have written my first chrome extension today. What I want it to do is open a tab in the background (pinned), and after the page in the tab finishes loading, I want the tab to close.

So far I have:

chrome.tabs.create({url: target, selected: false, pinned: true});

What the above code does is open the tab in the background, and pin it.

How do I close the tab once it has finished loading?

like image 326
ramo Avatar asked Sep 12 '25 11:09

ramo


1 Answers

chrome.tabs.create({url: target, selected: false, pinned: true}, myTab => {
    function listener(tabId, changeInfo, tab) {
        // make sure the status is 'complete' and it's the right tab
        if (tabId === myTab.id && changeInfo.status == 'complete') {
            chrome.tabs.remove(myTab.id);
            chrome.tabs.onUpdated.removeListener(listener);
        }
    };
    chrome.tabs.onUpdated.addListener(listener);
});
like image 145
Andrey Ptashinskiy Avatar answered Sep 15 '25 00:09

Andrey Ptashinskiy