Getting current browser url in Firefox Addon

firefox, firefox-addon, javascript

Solution

// you need to use this service first
var windowsService = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);

// window object representing the most recent (active) instance of Firefox
var currentWindow = windowsService.getMostRecentWindow('navigator:browser');

// most recent (active) browser object - that's the document frame inside the chrome
var browser = currentWindow.getBrowser();

// object containing all the data about an address displayed in the browser
var uri = browser.currentURI;

// textual representation of the actual full URL displayed in the browser
var url = uri.spec;

Problem

I within a panel and I want to get the current browser URL. Nothing so far works. Here's what I've tested: Only thing that even returns anything, I get something like `resource://jid0-18z0ptaugyu0arjkaoywztggyzg-at-jetpack/` and then my current panel resource. Obviously this is a scope problem but I don't know how to refer to the actual browser. ``` window.location.href ``` I've tried literally everything in the biggest Stack Overflow thread on this: Get current page URL from a firefox sidebar extension. None of them return anything. If it helps, I am using the Firefox Addon Builder.

Original source

Related problems