Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extensions : How to know when a tab has finished loading, from the background page

I'm using a listener in the background page to know when a tab is loaded:

chrome.tabs.onUpdated.addListener(function (tabId) { }) 

But the listener is fired twice: when the page has started loading, and when the page has finished.Is there a way to differentiate the two cases?

like image 771
Omiod Avatar asked Jan 27 '10 20:01

Omiod


People also ask

How do I keep tabs active in the background?

To make a background tab active in Google Chrome automatically, you need to do the following. Press and hold Ctrl + Shift keys together on the keyboard, and only then click the link that you want to switch to immediately. It will be opened in a new foreground tab. This trick should work in all Chromium-based browsers.

Do Chrome extensions run in background?

Depending on the extensions you have installed, sometimes Google Chrome will continue to run in the background on your computer after closing it. You might notice this, especially after setting up a new computer and when you install a fresh version of the browser.

Can I see when I opened a tab?

Ctrl + Shift + B Click on the word "History" in the left pane Right-click on a column header in the right pane, such as "Name" Left-click "Most Recent Visit", a check mark will then appear next to it, and you will now see that column You can drag the columns left or right, to place them as you wish Under "History" in ...


2 Answers

Luckily have found the solution.

There is an additional parameter that holds the status value:

chrome.tabs.onUpdated.addListener(function (tabId , info) {   if (info.status === 'complete') {     // your code ...   } }); 

Status can be either loading or complete.

like image 193
Omiod Avatar answered Oct 02 '22 16:10

Omiod


I wanted a easier way to do this after opening a tab

function createTab (url) {     return new Promise(resolve => {         chrome.tabs.create({url}, async tab => {             chrome.tabs.onUpdated.addListener(function listener (tabId, info) {                 if (info.status === 'complete' && tabId === tab.id) {                     chrome.tabs.onUpdated.removeListener(listener);                     resolve(tab);                 }             });         });     }); } 

so it would be

let tab = await createTab('http://google.com'); 
like image 41
Chad Scira Avatar answered Oct 02 '22 17:10

Chad Scira