WebExtensions : Issue with tab on created event

Hello, I am developping my first addon with WebExtensions API (available on AMO here and on GitHub here) and I am facing some issues with detecting new created tabs:

  1. In Firefox 56, when I click the “Restore previous session” button the event browser.tabs.onCreated is not triggered whereas when I restore tabs one by one this event is triggered. Moreover, there is neither browser.tabs.onRestored nor browser.sessions.onRestored events according to MDN.
  2. In Firefox Android >= 54, the event browser.webNavigation.onCreatedNavigationTarget is not triggered if we click on links contrary to Firefox Desktop and Chrome.

How can I detect when tabs are created/restored via the “Restore previous session” button?
Is there an alternative method to detect when a tab is created by a link in Android?

What do you mean by that?

Does Firefox maybe fire a .windows.onCreated event? If not, you should look for / report the bug on bugzilla.

In Firefox Android >= 54, the event browser.webNavigation.onCreatedNavigationTarget is not triggered if we click on links contrary to Firefox Desktop and Chrome.

It should work. If it really doesn’t ==> bugzilla. In the meantime, tabs.onCreated seems the best alternative.

I mean if I restore a specific tab in history (CTRL+SHIFT+T), onCreated is triggered, but not with “restore previous session”.

According to MDN, onCreatedNavigationTarget is only sent in the ‘window.open()’ case for Firefox Android. And with tabs.onCreated, how can I know if the tab is created by a link?

According to MDN, onCreatedNavigationTarget is only sent in the ‘window.open()’ case for Firefox Android.

Right. Why do they create an compatibility overwiew if it doesn’t list things like this (that is where I looked). -.-

know if the tab is created by a link

What is your definition of that, and what d you need it for?
I’m asking because at this point we are looking for hacks around buggy APIs - and those usually work only in specific cases.

For example, if you click on a link and it opens a new tab, onCreatedNavigationTarget is triggered and I can for example redirect url or remove this tab (this is the goal of my addon). But on Android, how can I know if the tab is created by clicking on a link ?

Doesn’t that also happen for links from bookmarks or external applications?

No, it happens only with links inside a web page.

Indeed. I have played with it a bit and it appears that the event is fired in Firefox 58 when:

  • mouse-wheel or Ctrl-clicking a link so it opens in a new tab
  • clicking (or .click()ing) a link with target=_blank, unless it also has rel=noopener
  • window.open() wasn’t intercepted by the popup blocker

So if you are doing some poup-blockerisch thing, I can see how onCreatedNavigationTarget is useful.

Unfortunately, I can’t come up with a workaround that isn’t major hacking.

Thank you for your help. Ok, this corresponds to what is written on MDN onCreatedNavigationTarget :

Fired when a new window, or a new tab in an existing window, is created to host the target of a navigation. For example, this event is sent when:

  • the user opens a link in a new tab or window (no Android support)
  • a web page loads a resource into a new tab or window using window.open() (but note that the event is not sent if the browser’s popup blocker blocks the load).

The event is not sent if a tab or window is created without a navigation target (for example, if the user opens a new tab by pressing Ctrl+T).

And what about “restore previous session”? Is there any method to know when and which tabs are restored?

I’m not sure if this is what you are looking for, but if an about:sessionrestore tab is closed, you could poll for new tabs shortly afterwards.

According to MDN, about:sessionrestore is displayed only after a Firefox crash :confused: the only tab which is removed is about:home on session restore. Still no solution… I think I will report it on bugzilla, I didn’t find a similar case.

Bug reported on https://bugzilla.mozilla.org/show_bug.cgi?id=1405249.
I am still looking for an alternative short-term solution :thinking:

After doing several tests, browser.tabs.onCreated seems to be triggered randomly on previous session restore on Firefox. I hope that issue will be fixed in next versions. Nevertheless, I found a little workaround :slight_smile: browser.tabs.onUpdated is triggered on session restore so I use both onCreated and onUpdated to be sure to detect new created tabs.
Here is an example :


var tablist = [];

browser.tabs.onCreated.addListener(function(tab){
    tablist.push(tab);
});

browser.tabs.onUpdated.addListener(function(tabid,info,tab){
    if(!tablist.find(function(val){return (val.id==tabid)})) tablist.push(tab);
});