gBrowser.tabContainer is undefined? Topic is solved
Moderators: FranklinDM, Lootyhoof
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
gBrowser.tabContainer is undefined?
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?
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.
Eat your drugs.
Eat your teeth.
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
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.
Eat your drugs.
Eat your teeth.
-
- Keeps coming back
- Posts: 940
- Joined: 2021-01-26, 11:18
Re: gBrowser.tabContainer is undefined?
Most likely you are using something in the wrong way.
-
- Moon Magic practitioner
- Posts: 2194
- Joined: 2018-05-05, 13:29
Re: gBrowser.tabContainer is undefined?
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.
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
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);
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.
Eat your drugs.
Eat your teeth.
-
- Add-ons Team
- Posts: 582
- Joined: 2017-01-14, 02:40
- Location: Philippines
Re: gBrowser.tabContainer is undefined?
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.
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
tabContainer is still randomly nulling thoFranklinDM wrote: ↑2024-02-23, 05:55Just 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.
I can't create a progress listener for null.
(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.
Eat your drugs.
Eat your teeth.
-
- Add-ons Team
- Posts: 582
- Joined: 2017-01-14, 02:40
- Location: Philippines
Re: gBrowser.tabContainer is undefined?
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.
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
But tabContainer is normally available, just until the browser crashes and restarts or does something else to restore the session.FranklinDM wrote: ↑2024-03-03, 16:49The 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.
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.
Eat your drugs.
Eat your teeth.
-
- Keeps coming back
- Posts: 940
- Joined: 2021-01-26, 11:18
Re: gBrowser.tabContainer is undefined?
Try to do as FranklinDM said...
Code: Select all
gBrowser.addProgressListener({ onLocationChange: function() { Flashify.BrowserOverlay.onPageLoad(); } });
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
For some reason I didn't get a notification, sorry for the late reply.Kris_88 wrote: ↑2024-03-04, 17:52Try to do as FranklinDM said...Code: Select all
gBrowser.addProgressListener({ onLocationChange: function() { Flashify.BrowserOverlay.onPageLoad(); } });
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.
Eat your drugs.
Eat your teeth.
-
- Knows the dark side
- Posts: 4984
- Joined: 2015-12-09, 15:45
Re: gBrowser.tabContainer is undefined?
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:PseudoDistant wrote: ↑2024-03-05, 05:31I never actually remove listeners, but I don't know how to do that in an appropriate manner.
Code: Select all
mynamespace={
//extension functions here
mycleanupfunction : function(){
//remove listeners here
}
}
window.addEventListener("unload", mycleanupfunction, false);
"One hosts to look them up, one DNS to find them and in the darkness BIND them."
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX
-
- Keeps coming back
- Posts: 940
- Joined: 2021-01-26, 11:18
Re: gBrowser.tabContainer is undefined?
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).
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
I'm testing primarily on Basilisk, idk if that has any impact, but it seems to not do anything at all.Kris_88 wrote: ↑2024-03-05, 10:25I 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).
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.
Eat your drugs.
Eat your teeth.
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
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.moonbat wrote: ↑2024-03-05, 07:25You 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:
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.Code: Select all
mynamespace={ //extension functions here mycleanupfunction : function(){ //remove listeners here } } window.addEventListener("unload", mycleanupfunction, false);
Eat your school.
Eat your drugs.
Eat your teeth.
Eat your drugs.
Eat your teeth.
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
Nevermind, just breaks slightly less often now.PseudoDistant wrote: ↑2024-03-05, 14:12This actually seems to have fixed the entire issue. Even if I unplug my computer (to force the session restore), it still works. Thank you.
Eat your school.
Eat your drugs.
Eat your teeth.
Eat your drugs.
Eat your teeth.
-
- Keeps coming back
- Posts: 940
- Joined: 2021-01-26, 11:18
Re: gBrowser.tabContainer is undefined?
It works in Basilisk as well.PseudoDistant wrote: ↑2024-03-05, 14:10I'm testing primarily on Basilisk, idk if that has any impact, but it seems to not do anything at all.
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");
}});
-
- Moonbather
- Posts: 73
- Joined: 2024-02-06, 20:19
Re: gBrowser.tabContainer is undefined?
After trying yet again, and smashing my skull into the wall for the 43rd time today, I figured out what was going wrong.Kris_88 wrote: ↑2024-03-05, 20:11It 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"); }});
Code: Select all
gBrowser.contentDocument.getElementById(...) is null
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.
Eat your drugs.
Eat your teeth.
-
- Keeps coming back
- Posts: 940
- Joined: 2021-01-26, 11:18
Re: gBrowser.tabContainer is undefined?
Probably onStateChange, but I've never used that. This needs to be experimented...
https://udn.realityripple.com/docs/Arch ... _Listeners
-
- Keeps coming back
- Posts: 940
- Joined: 2021-01-26, 11:18
Re: gBrowser.tabContainer is undefined?
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...