Is pendingURL always defined for tabs.onCreated(function(tab){})?

限于喜欢 提交于 2020-04-18 02:38:54

问题


Will the pendingURL property always be defined for tab argument of a tabs.onCreated callback function? I'm trying to decide whether or not I also need to check tab.url, as in this

chrome.tabs.onCreated.addListener(function (newTab)
{
  if(newTab.pendingUrl === extensionURL || newTab.url === extensionURL) 
  {
    //...
  }
}

Thanks for any insight you can give.

This is my first question here so any feedback on how to better post questions is welcome.


回答1:


It's defined only when there's a pending navigation that didn't resolve visually in the browser's address bar. Once Chrome decided to proceed (which happens after finally connecting to the remote server), it'll change the URL in the address bar and that'll be the end of "pending".

There's no guarantee whether it'll be present or not because it depends on the asynchronous events in the network stack and the way the OS schedules processes at this particular moment. The Chromium's source code simply has an if check there so it doesn't assume anything.

Do it like this:

if ((newTab.pendingUrl || newTab.url) === extensionURL) {
  //....
}

or

const url = newTab.pendingUrl || newTab.url;
if (url === extensionURL) {
  //....
}


来源:https://stackoverflow.com/questions/60443329/is-pendingurl-always-defined-for-tabs-oncreatedfunctiontab

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