问题
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