The tab.onCreated
event always provides a tab object with, confusingly, url: "about:blank"
and status: "complete"
no matter what even if the tab was opened from a link.
Yes, the documentation notes this behaviour and suggests you then also listen for tab.onUpdated
events (possibly more than one) to get the final tab object.
I would love to have all this work abstracted away into a single onTabCreatedFinal
sort of meta-listener-function that spits out the final tab object of a newly created tab, to make life easier. How would you go about doing this?
Oh man, tell me about it… It’s just terrible, not to mention it behaves differently in different browsers.
However there may be an easy workaround, depending on what you are trying to do.
I needed my addon to work on user-specified pages only. However observing from the background page when the tab is loaded or reloaded or updated is just tricky.
So instead I’ve registered content script that loads into every page and has only this line:
browser.runtime.sendMessage({type: 'page_loaded', hostname: location.hostname});
So every time a page is loaded, I’ll get a message in my background script where I easily check if the URL matches or not.
Nice workaround @juraj.masiar.
I myself have been investigating, and noticed something… The onCreated tab object’s title
property seems to contain the url (albeit sans protocol), unless it’s a blank or default-new tab. This is good enough for my case, I think! I only need to check if a tab has a url, and if it has a parent tab, which is informed by the openerTabId
property.
I hope this is consistent behaviour; I’ve so far only checked in FF 86 on Linux Mint.
BTW I don’t mind seeing other solutions – perhaps ones that involve onUpdated – to learn from in this thread’s future anytime
(Doesn’t have to be a single onTabCreatedFinal-type thing of course, just any good reusable pattern would do)