I am making a Firefox add-on that will log how long the user keeps a webpage open in his browser.
I was able to catch the link of every new webpage opening in the browser (both in new tab or in already opened one) by adding a progress listener:
addProgressListener(myAddon_urlBarListener,
Components.interfaces.nsIWebProgress.NOTIFY_ALL);
Then I use onLocationChange to get the new URL for a tab.
The problem is that I don’t know the old URL for the tab to be able to stop the timer and to calculate the duration for that webpage. Right now I am only able to calculate time if I close the tab of the webpage because I am able to catch closing tab event and what is the link in the page before closing.
I need a way to catch the URL of old webpage if I open an new URL in the same tab, and this needs to work even if more that one tab is opened in the browser. Btw, I am not working with jQuery but if it is required I will.
What if the same URL is opened in two tabs? You should be focusing on tabs, not URLs. Your
onLocationChangefunction should find the<browser>element associated with the tab and just remember its URL (as long as the time). Next time you see the same<browser>element you will have all the information you need. Best way to store data about the browser is currentlyWeakMap, and finding the<browser>element is possible usingtabbrowser.getBrowserForDocument()method. So something like this should work:Of course you will still have to handle
TabCloseevent. There you will needtabbrowser.getBrowserForTab()method to find the<browser>element by its corresponding<tab>element.