chrome.tabs.onupdated.addlistener doesn't work on cached pages

不羁岁月 提交于 2019-12-24 14:53:08

问题


chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (tab.url.indexOf('http') == '0' && changeInfo.status == 'complete') {
        alert(JSON.stringify(changeInfo));
    }
});

i have such background.js it nearly works always %99 but not %100

whenever i restart my browser and enter an already accessed web page when browser loads all contents from cache it doesn't fire this chrome.tabs event

but the interesting part is when i click same website from dial speed it doesn't load from cache and calls the "tabs." action


回答1:


According to the docs https://developer.chrome.com/extensions/webNavigation

Not all navigating tabs correspond to actual tabs in Chrome's UI, e.g., a tab that is being pre-rendered. Such tabs are not accessible via the tabs API nor can you request information about them via webNavigation.getFrame or webNavigation.getAllFrames. Once such a tab is swapped in, an onTabReplaced event is fired and they become accessible via these APIs.

and

If a navigation was triggered via Chrome Instant or Instant Pages, a completely loaded page is swapped into the current tab. In that case, an onTabReplaced event is fired.

Try using the chrome.webNavigation.onTabReplaced event to catch this:

chrome.webNavigation.onTabReplaced.addListener(function (details) {
   chrome.tabs.get(details.tabId, function(tab) {
       console.log(tab.url);
   });
});

Note: You'll need the "webNavigation" permission in the chrome extension manifest:

{
    "name": "My extension",
    ...
    "permissions": [
      "webNavigation"
    ],
    ...
  }



回答2:


I tested this with 25 open tabs using:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
    console.log("update: "+JSON.stringify(changeInfo));
});

On restarting my browser I received 25 "loading" events, but only 23 "completes". On restarting my browser a second time I received 25 "loading" events, but only 22 "completes".

I don't think changeInfo.status == 'complete' will always follow a pageload, especially after restarting.

consider using chrome.tabs.query() right after your extension loads instead.

chrome.tabs.query({}, function(tabs) {
    for(var i=0;i<tabs.length;i++){
        if (tabs[i].url.indexOf('http') == '0') {
            dostuff();
        }
    }
});


来源:https://stackoverflow.com/questions/36187908/chrome-tabs-onupdated-addlistener-doesnt-work-on-cached-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!