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

前端 未结 2 1371
Happy的楠姐
Happy的楠姐 2020-12-05 04:29

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

chrome.tabs.onUpdated.addListener(function          


        
相关标签:
2条回答
  • 2020-12-05 04:47

    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.

    0 讨论(0)
  • 2020-12-05 04:49

    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');
    
    0 讨论(0)
提交回复
热议问题