Get the active tab's URL using onActivated

143 views
Skip to first unread message

Cana Ben Haim

unread,
Jul 4, 2023, 2:34:52 AM7/4/23
to Chromium Extensions
Hi everyone,
I am using the chrome.tabs.onActivated event to get the currently active tab's URL:

Screenshot 2023-07-04 at 9.29.41.png

I saw in the formal documentation that the URL might not be available when the event fires:
Screenshot 2023-07-03 at 10.25.42.png

What is the reason for this behavior? In which cases this happens?
Since I am using the chrome.tabs.query to get all the tabs and then looking for the active one, is it safe to assume the URL would be set by then?
Thank you!

Deco

unread,
Jul 4, 2023, 3:04:45 AM7/4/23
to Cana Ben Haim, Chromium Extensions
It isn't set because the tab may not be loaded fully before the event is fired, and if it's not, then the Promise will not be able to get the URL.

It isn't safe to assume that the URL will be set by the time the query is fired either, for the same reason as above.

If you want this behaviour explicitly for having the Promise when the URL is set, you can use chrome.tabs.onUpdated and listen for when the URL is updated (e.g. set/not set), this should avoid the issue you're facing.

Cheers,
Deco

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/aa77cf9b-8a5d-4c11-9d61-6431b68d0a48n%40chromium.org.

Cana Ben Haim

unread,
Jul 4, 2023, 3:53:41 AM7/4/23
to Chromium Extensions, Deco, Chromium Extensions
Ok, I understand.
The problem is that onUpdated event doesn't fire an event when the active tab is being changed (which is what I need). It only works when opening a new tab or reloading an existing one.
Are there any more alternatives for getting the currently active tab's URL?

hrg...@gmail.com

unread,
Jul 4, 2023, 5:36:33 AM7/4/23
to Chromium Extensions, Cana Ben Haim, Deco, Chromium Extensions
The problem is that onUpdated event doesn't fire an event when the active tab is being changed

This is not correct. The onUpdated event fires whenever the URL of any tab changes.

Cana Ben Haim

unread,
Jul 4, 2023, 7:24:23 AM7/4/23
to Chromium Extensions, hrg...@gmail.com, Cana Ben Haim, Deco, Chromium Extensions
Still, this is not what I'm looking for.
I want to know which tab is currently active, or in different words - which tab is currently viewed by the user.

For example, if you have multiple tabs that are already open, and you click on one of them - nothing changes. Everything stays the same, including the URL, except for the fact that this tab is currently the active one. And in this case, onUpdate doesn't fire any event. 

Deco

unread,
Jul 4, 2023, 11:23:59 AM7/4/23
to Cana Ben Haim, Chromium Extensions, hrg...@gmail.com
What you are wanting now, is not the same as you had specified originally, and therefore I'm not really sure what you are actually requesting. The onActivated listener is explicitly used for this exact purpose.

Cheers,
Deco
Reply all
Reply to author
Forward
0 new messages