Detecting current tab URL

Talk about code development, features, specific bugzilla bugs, enhancements, patches, and other highly technical things.
Forum rules
Please keep everything here strictly on-topic.
This board is meant for Pale Moon source code development related subjects only like code snippets, patches, specific referenced Bugzilla bugs, mercurial, etc.

This is not for tech support! Please do not post tech support questions in the "Development" board!
Please make sure not to use this board for support questions. Most "bug reports" do not belong in this board and should initially be posted in Community Support or other relevant support boards.

Please keep things on-topic as this forum will be used for reference for Pale Moon development. Expect topics that aren't relevant as such to be moved or deleted.
Post Reply
User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1076
Joined: 2015-12-09, 15:45
Location: Australia

Detecting current tab URL

Post by moonbat » 2020-01-10, 10:28

I'm trying to make an overlay extension to play Youtube videos in a separate window.
I want my overlay extension's toolbar button and menu item to only be enabled on the Youtube website. This can happen either when the user manually loads Youtube in the current tab, or switches to another one that already has it open. To this end, it has to monitor both when the current tab loads and when the user switches the active tab and then read the URL. I've added a listener for TabSelect for the latter, how do I detect the former?

If I only listen to TabSelect and switch to an inactive tab (say from my previous session, one that hasn't been loaded yet), then the URL returned is 'about:blank'. Only if I switch away and back to it after it's loaded does it show the URL. How do I also listen for load events on the tab? Just adding "load" doesn't work, that seems to apply only to top level windows.

I'm doing it like so:

Code: Select all

        var container = gBrowser.tabContainer;

        container.addEventListener("TabSelect",PMPlayer.Launch.checkVideoURL,false);
checkVideoURL looks like this -

Code: Select all

    checkVideoURL:function(event){
        var browser=gBrowser.selectedBrowser;
        URI = browser.currentURI;
        PMPlayer.Debug.log("Event type = "+event.type+"\nCurrent URI is "+URI.spec);
    }
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 19.3 Xfce x64 on HP i5 laptop with 4 GB RAM, always latest versions of PM & Basilisk unless specified.

vannilla
Keeps coming back
Keeps coming back
Posts: 826
Joined: 2018-05-05, 13:29

Re: Detecting current tab URL

Post by vannilla » 2020-01-10, 12:15

I can't help with actual experience, but I found these resources:
https://developer.mozilla.org/en-US/doc ... ed_browser
and
https://developer.mozilla.org/en-US/doc ... n_all_tabs

Maybe you already know them, but I suppose it's better than nothing.

User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1076
Joined: 2015-12-09, 15:45
Location: Australia

Re: Detecting current tab URL

Post by moonbat » 2020-01-10, 12:34

vannilla wrote:
2020-01-10, 12:15
I can't help with actual experience, but I found these resources:
https://developer.mozilla.org/en-US/doc ... ed_browser
and
https://developer.mozilla.org/en-US/doc ... n_all_tabs

Maybe you already know them, but I suppose it's better than nothing.
I know, I used those snippet to get started. Thanks anyhow.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 19.3 Xfce x64 on HP i5 laptop with 4 GB RAM, always latest versions of PM & Basilisk unless specified.

User avatar
Isengrim
Board Warrior
Board Warrior
Posts: 1058
Joined: 2015-09-08, 22:54
Location: 127.0.0.1
Contact:

Re: Detecting current tab URL

Post by Isengrim » 2020-01-10, 12:52

Reader View does something similar, updating its button in several cases:

Code: Select all

gBrowser.addProgressListener(this.browserWindowListener);
gBrowser.addTabsProgressListener(this.tabsProgressListener);
window.addEventListener("aftercustomization", this.onCustomizeEnd, false);

// Update the button on actual change of the URL.
browserWindowListener: {
  onLocationChange(aWebProgress, aRequest, aLocationURI, aFlags) {
    // Do things
  }
}

// Update the button on anchor navigation and history change.
tabsProgressListener: {
  onLocationChange(aBrowser, aWebProgress, aRequest, aLocationURI, aFlags) {
    // Do things
  }
}

// Update the button after customization.
onCustomizeEnd(aEvent) {
  // Do things
}
So instead of trying to listen to events on the tabcontainer, just use the Progress Listeners on the gbrowser object. I guess it's the same thing, but the code is a little simpler.
Linux Mint 19.2 Cinnamon (64-bit), Windows 7 (64-bit), Windows 10 build 1803 (64-bit)
"As long as there is someone who will appreciate the work involved in the creation, the effort is time well spent." ~ Tetsuzou Kamadani, Cave Story

User avatar
JustOff
Localization Coordinator
Localization Coordinator
Posts: 1896
Joined: 2015-09-03, 19:47
Location: UA
Contact:

Re: Detecting current tab URL

Post by JustOff » 2020-01-10, 13:04

Here are the add-ons I made in a spare time. That was fun!

User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1076
Joined: 2015-12-09, 15:45
Location: Australia

Re: Detecting current tab URL

Post by moonbat » 2020-01-10, 13:12

Thanks to both of you, just landed on this page. I'll take a look. One other thing - is it possible to make an icon look greyed out for being disabled using CSS tricks - or do I need a separate greyed out icon image for it?
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 19.3 Xfce x64 on HP i5 laptop with 4 GB RAM, always latest versions of PM & Basilisk unless specified.

User avatar
Isengrim
Board Warrior
Board Warrior
Posts: 1058
Joined: 2015-09-08, 22:54
Location: 127.0.0.1
Contact:

Re: Detecting current tab URL

Post by Isengrim » 2020-01-10, 13:26

You might be able to use CSS tricks if your image is an SVG, I'm not sure. I usually use two icons - a colored one and a grayscale one. If you already have a colored icon, you can use any number of free graphics editing programs to create a grayscale one.
Linux Mint 19.2 Cinnamon (64-bit), Windows 7 (64-bit), Windows 10 build 1803 (64-bit)
"As long as there is someone who will appreciate the work involved in the creation, the effort is time well spent." ~ Tetsuzou Kamadani, Cave Story

User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1076
Joined: 2015-12-09, 15:45
Location: Australia

Re: Detecting current tab URL

Post by moonbat » 2020-01-10, 13:30

Isengrim wrote:
2020-01-10, 13:26
You might be able to use CSS tricks if your image is an SVG, I'm not sure. I usually use two icons - a colored one and a grayscale one. If you already have a colored icon, you can use any number of free graphics editing programs to create a grayscale one.
Thanks. I'll play around with GIMP in that case.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 19.3 Xfce x64 on HP i5 laptop with 4 GB RAM, always latest versions of PM & Basilisk unless specified.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 25768
Joined: 2011-08-28, 17:27
Location: 58°2'16"N 14°58'31"E
Contact:

Re: Detecting current tab URL

Post by Moonchild » 2020-01-10, 13:46

There is also css filter to do it on the fly
"There will be times when the position you advocate, no matter how well framed and supported, will not be accepted by the public simply because you are who you are." -- Merrill Rose
Image

User avatar
moonbat
Board Warrior
Board Warrior
Posts: 1076
Joined: 2015-12-09, 15:45
Location: Australia

Re: Detecting current tab URL

Post by moonbat » 2020-01-16, 01:03

Moonchild wrote:
2020-01-10, 13:46
There is also css filter to do it on the fly
Figured a better way - hiding the button and menu option on pages where they aren't applicable. So they will only show up if the current tab is on Youtube.

I had a couple of other questions for stuff I couldn't figure out from the XUL documentation - should I post them here or start separate threads?
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 19.3 Xfce x64 on HP i5 laptop with 4 GB RAM, always latest versions of PM & Basilisk unless specified.

Post Reply