gBrowser.tabContainer is undefined? Topic is solved

Add-ons for Pale Moon and other applications
General discussion, compatibility, contributed extensions, themes, plugins, and more.

Moderators: FranklinDM, Lootyhoof

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-02-17, 20:36

I don't know what happened, my extension code for Flashify worked fine yesterday, but now all of a sudden it's throwing "TypeError: gBrowser.tabContainer is undefined"...
The browser didn't update, I didn't change anything in Flashify today, it just randomly broke.
Am I not meant to use gBrowser.tabContainer?
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-02-22, 14:52

It seems like the bug is related to restoring tabs. If I tell Basilisk to restore tabs from the last session, it seems to occur randomly. However, if Basilisk doesn't restore tabs from last session, the issue never occurs. I think this is a browser bug.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 940
Joined: 2021-01-26, 11:18

Re: gBrowser.tabContainer is undefined?

Unread post by Kris_88 » 2024-02-22, 16:34

Most likely you are using something in the wrong way.

vannilla
Moon Magic practitioner
Moon Magic practitioner
Posts: 2194
Joined: 2018-05-05, 13:29

Re: gBrowser.tabContainer is undefined?

Unread post by vannilla » 2024-02-22, 22:24

PseudoDistant wrote:
2024-02-22, 14:52
It seems like the bug is related to restoring tabs.
Depending on when your code is executed it might happen that tabs are not yet fully initialized after a session restore.
I think you can listen to some event to know if you can execute your code in this case, but I don't really remember which specifically.

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-02-23, 02:07

vannilla wrote:
2024-02-22, 22:24
Depending on when your code is executed it might happen that tabs are not yet fully initialized after a session restore.
I think you can listen to some event to know if you can execute your code in this case, but I don't really remember which specifically.
After it reaches this state, it continues to not work until I uninstall and reinstall the extension (or close all tabs and restart).
All that said I'm not an expert at XUL, nor will I pretend to be. I have two events:

Code: Select all

gBrowser.tabContainer.addEventListener("TabSelect", function() { Flashify.BrowserOverlay.onPageLoad(); }, true);
window.addEventListener("load", function() { Flashify.BrowserOverlay.init(); }, false);
Not sure if that's the issue or not.

EDIT: I also don't ever remove the event listeners, I'm not sure why I would need to for what I'm doing specifically, and I've seen quite a few extensions not do that. Perhaps that's the issue? I'm still kind of learning the ropes a bit.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
FranklinDM
Add-ons Team
Add-ons Team
Posts: 582
Joined: 2017-01-14, 02:40
Location: Philippines

Re: gBrowser.tabContainer is undefined?

Unread post by FranklinDM » 2024-02-23, 05:55

Just a suggestion: instead of relying on tab events for executing code on page load, you might want to try adding a progress listener to gBrowser instead. Swarth uses the same method as well.

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-03, 16:12

FranklinDM wrote:
2024-02-23, 05:55
Just a suggestion: instead of relying on tab events for executing code on page load, you might want to try adding a progress listener to gBrowser instead. Swarth uses the same method as well.
tabContainer is still randomly nulling tho :cry:
I can't create a progress listener for null. :cry:
(I don't mean to sound rude, I'm still learning so any suggestion helps, this issue is just really frustrating.)

I could resolve it by just not restoring sessions, but if the browser crashes for any reason, or I forget to close it when I shut my laptop down (KDE session restore pls), I still have the same issue.
Not using a feature isn't really a "fix", moreso a bandaid.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
FranklinDM
Add-ons Team
Add-ons Team
Posts: 582
Joined: 2017-01-14, 02:40
Location: Philippines

Re: gBrowser.tabContainer is undefined?

Unread post by FranklinDM » 2024-03-03, 16:49

PseudoDistant wrote:
2024-03-03, 16:12
tabContainer is still randomly nulling tho :cry:
The approach in the page I've linked adds a progress listener to gBrowser itself, not gBrowser.tabContainer, which IIRC should be available at that time.

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-04, 15:43

FranklinDM wrote:
2024-03-03, 16:49
PseudoDistant wrote:
2024-03-03, 16:12
tabContainer is still randomly nulling tho :cry:
The approach in the page I've linked adds a progress listener to gBrowser itself, not gBrowser.tabContainer, which IIRC should be available at that time.
But tabContainer is normally available, just until the browser crashes and restarts or does something else to restore the session.

I'm trying to handle redirects and such, as well. Do progress listeners actually handle that? I found tabContainer from looking at UDN iirc.

EDIT: Oh yea I also need TabContainer because I need to be able to listen for if the user switches tabs.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 940
Joined: 2021-01-26, 11:18

Re: gBrowser.tabContainer is undefined?

Unread post by Kris_88 » 2024-03-04, 17:52

Try to do as FranklinDM said...

Code: Select all

gBrowser.addProgressListener({ onLocationChange: function() { Flashify.BrowserOverlay.onPageLoad(); } });

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-05, 05:31

Kris_88 wrote:
2024-03-04, 17:52
Try to do as FranklinDM said...

Code: Select all

gBrowser.addProgressListener({ onLocationChange: function() { Flashify.BrowserOverlay.onPageLoad(); } });
For some reason I didn't get a notification, sorry for the late reply.
I had already tried, it didn't seem to work. Sorry for not clarifying.
The tabContainer event is specifically just for detecting when the user switches tabs. It's responsible for changing the icon color. (I grey the icon out when on an unsupported site.)

I think it might be more related to the fact that I never actually remove listeners, but I don't know how to do that in an appropriate manner. I'm not positive on that though, as I saw a couple other extensions that never remove event listeners and don't have these issues.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4984
Joined: 2015-12-09, 15:45

Re: gBrowser.tabContainer is undefined?

Unread post by moonbat » 2024-03-05, 07:25

PseudoDistant wrote:
2024-03-05, 05:31
I never actually remove listeners, but I don't know how to do that in an appropriate manner.
You must remove them; this is how extensions cause memory leaks by not cleaning up listeners after them. Many older Firefox extensions are guilty of this. To do this, add an unload event listener on the window object in the main overlay script file and call your cleanup function in it, like so:

Code: Select all

mynamespace={

//extension functions here

  mycleanupfunction : function(){
     //remove listeners here
  }
}
window.addEventListener("unload", mycleanupfunction, false);
Always use a unique namespace to hold all your extension's functions so that their names and variable names don't conflict with those of other extensions or the browser itself, since everything runs in one Javascript environment.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 940
Joined: 2021-01-26, 11:18

Re: gBrowser.tabContainer is undefined?

Unread post by Kris_88 » 2024-03-05, 10:25

PseudoDistant wrote:
2024-03-05, 05:31
I had already tried, it didn't seem to work.
I use this event (addProgressListener, onLocationChange) in my addons. It fires both when switching tabs and when navigating in any tab.
In the event handler I use gBrowser.selectedTab to determine the current tab.
As for gBrowser.tabContainer, this is a reference to the DOM element of the tabs container, and it is possible that this element could be temporarily detached from the tabbrowser in some situations (and this is even logical when restoring a session).

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-05, 14:10

Kris_88 wrote:
2024-03-05, 10:25
I use this event (addProgressListener, onLocationChange) in my addons. It fires both when switching tabs and when navigating in any tab.
In the event handler I use gBrowser.selectedTab to determine the current tab.
As for gBrowser.tabContainer, this is a reference to the DOM element of the tabs container, and it is possible that this element could be temporarily detached from the tabbrowser in some situations (and this is even logical when restoring a session).
I'm testing primarily on Basilisk, idk if that has any impact, but it seems to not do anything at all.

It's entirely possible I'm doing something very wrong, and if I happen to be I'm sorry, I'm extremely unstable right now.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-05, 14:12

moonbat wrote:
2024-03-05, 07:25
You must remove them; this is how extensions cause memory leaks by not cleaning up listeners after them. Many older Firefox extensions are guilty of this. To do this, add an unload event listener on the window object in the main overlay script file and call your cleanup function in it, like so:

Code: Select all

mynamespace={

//extension functions here

  mycleanupfunction : function(){
     //remove listeners here
  }
}
window.addEventListener("unload", mycleanupfunction, false);
Always use a unique namespace to hold all your extension's functions so that their names and variable names don't conflict with those of other extensions or the browser itself, since everything runs in one Javascript environment.
This actually seems to have fixed the entire issue. Even if I unplug my computer (to force the session restore), it still works. Thank you. :angel:
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-05, 19:14

PseudoDistant wrote:
2024-03-05, 14:12
This actually seems to have fixed the entire issue. Even if I unplug my computer (to force the session restore), it still works. Thank you. :angel:
Nevermind, just breaks slightly less often now.
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 940
Joined: 2021-01-26, 11:18

Re: gBrowser.tabContainer is undefined?

Unread post by Kris_88 » 2024-03-05, 20:11

PseudoDistant wrote:
2024-03-05, 14:10
I'm testing primarily on Basilisk, idk if that has any impact, but it seems to not do anything at all.
It works in Basilisk as well.
Paste it into your code in the appropriate place and take a look...

Code: Select all

    gBrowser.addProgressListener({ onLocationChange: function() {
      Services.prompt.alert(null, "OLC", "OLC");
    }});

User avatar
PseudoDistant
Moonbather
Moonbather
Posts: 73
Joined: 2024-02-06, 20:19

Re: gBrowser.tabContainer is undefined?

Unread post by PseudoDistant » 2024-03-05, 21:21

Kris_88 wrote:
2024-03-05, 20:11
It works in Basilisk as well.
Paste it into your code in the appropriate place and take a look...

Code: Select all

    gBrowser.addProgressListener({ onLocationChange: function() {
      Services.prompt.alert(null, "OLC", "OLC");
    }});
After trying yet again, and smashing my skull into the wall for the 43rd time today, I figured out what was going wrong.

Code: Select all

gBrowser.contentDocument.getElementById(...) is null
The event listener for tabContainer fires late enough that I have access to the document.
If I use the progress listener instead, I fire too early, which leads to the icon change not working (because I automatically embed on my test setup.)
It does work, it's just my code not working well with it.
Can I have it not fire if the document isn't ready? (window.addEventListener("load", ...) handles that already).
Eat your school.
Eat your drugs.
Eat your teeth.

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 940
Joined: 2021-01-26, 11:18

Re: gBrowser.tabContainer is undefined?

Unread post by Kris_88 » 2024-03-05, 21:56

PseudoDistant wrote:
2024-03-05, 21:21
Can I have it not fire if the document isn't ready?
Probably onStateChange, but I've never used that. This needs to be experimented...
https://udn.realityripple.com/docs/Arch ... _Listeners

User avatar
Kris_88
Keeps coming back
Keeps coming back
Posts: 940
Joined: 2021-01-26, 11:18

Re: gBrowser.tabContainer is undefined?

Unread post by Kris_88 » 2024-03-05, 22:07

In general, the best documentation is the browser source code. And there is a certain set of methods and techniques for performing typical actions. There are a lot of subtleties and actually doing it right (that is, in the shortest and most reliable method, as browser developers would do) is not so easy...