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

**URL:** https://discourse.mozilla.org/t/webextensions-determine-url-from-browser-tab-gets-url-from-previous-domain/33127
**Category:** Development
**Tags:** webextensions-api
**Created:** [November 6, 2018, 3:41pm UTC](https://discourse.mozilla.org/t/webextensions-determine-url-from-browser-tab-gets-url-from-previous-domain/33127 "2018-11-06T15:41:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dieter](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dieter](https://discourse.mozilla.org/u/dieter)
#### Post date: [November 6, 2018, 3:41pm UTC](https://discourse.mozilla.org/t/webextensions-determine-url-from-browser-tab-gets-url-from-previous-domain/33127/1 "2018-11-06T15:41:55Z")

</div>

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](http://www.firefox.com) (page is redirected to [Get Firefox for desktop — Mozilla (US)](https://www.mozilla.org/en-US/firefox/new/?utm_medium=referral&utm_source=firefox-com)).
2. Assume the new [Mozilla.org](http://Mozilla.org) page needs 1 second to load
3. Our add-on must create a record like “[www.firefox.com](http://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](http://www.firefox.com) in my example)?

---

<div class="post-metadata">

### Author: ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)
#### Post date: [November 8, 2018, 5:24pm UTC](https://discourse.mozilla.org/t/webextensions-determine-url-from-browser-tab-gets-url-from-previous-domain/33127/2 "2018-11-08T17:24:34Z")

</div>

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`.)

---

<div class="post-metadata">

### Author: ![dieter](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@dieter](https://discourse.mozilla.org/u/dieter)
#### Post date: [November 9, 2018, 7:23am UTC](https://discourse.mozilla.org/t/webextensions-determine-url-from-browser-tab-gets-url-from-previous-domain/33127/3 "2018-11-09T07:23:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Baptistou](https://avatars.discourse-cdn.com/v4/letter/b/94ad74/32.png) [@Baptistou](https://discourse.mozilla.org/u/Baptistou)
#### Post date: [November 10, 2018, 11:54am UTC](https://discourse.mozilla.org/t/webextensions-determine-url-from-browser-tab-gets-url-from-previous-domain/33127/4 "2018-11-10T11:54:26Z")

</div>

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