WebExtensions: Determine URL from browser tab gets URL from previous domain

webextensions-api

(Dieter Schmitz) #1

Hi,

with our add-on, we want to determine page load times using WebExtensions API.
If a page is redirected we need to know the originally entered URL.

Let’s give an example:

  1. Load www.firefox.com (page is redirected to https://www.mozilla.org/en-US/firefox/new/?utm_medium=referral&utm_source=firefox-com).
  2. Assume the new Mozilla.org page needs 1 second to load
  3. Our add-on must create a record like “www.firefox.com;1000”
    The “1000” at the end is the page load time in milliseconds.

To determine the original (not redirected URL) we add a listener to the onBeforeRequest event. Within the callback function, we determine the URL from the tab and not from the given parameter as the URL within the parameter is not always the same URL seen in the address bar.
We are using the following code snippet in the callback function to determine the URL (“details” is the parameter of the callback function):

var tabUrl;
browser.tabs.get (details.tabId, function (tab)
{
// Null or undefined
if (tab == null)
return;

 try
 {
    tabUrl = new URL (tab.url);
 }
 catch (e) {return;}

});

And here is the problem:
For the main frame the variable tabUrl contains the URL from the page you are coming from (and not the URL of the address bar).

Does anybody know how to get the originally entered (not redirected) URL (www.firefox.com in my example)?


(Niklas Gollenstede) #2

Is there a reason why you aren’t using the url property of the onBeforeRequest listeners first argument? (I guess in your incomplete example that would be details.url.)


(Dieter Schmitz) #3

We cannot use the details.url parameter, as this URL is not always the URL shown in the address bar (can be different within an iframe).
The onBeforeRequest event is thrown for each request within a page including IFrames.


(Baptiste Thémine) #4

Add a listener to browser.tabs.onUpdated to get the current URL.