Reputation: 95
So I've collected 5 different methods to do this, none of which work from within a panel. Firefox is stunningly effective at blocking access to a basic task.
Here's what I've tried:
Attempt 1:
var url = window.top.getBrowser().selectedBrowser.contentWindow.location.href;
Error: window.top.getBrowser is not a function
Attempt 2:
var url = window.content.document.location;
Error: Permission denied to access property 'document'
Attempt 3:
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
var url = mainWindow.getBrowser().selectedBrowser.contentWindow.location.href;
Error: Permission denied to create wrapper for object of class UnnamedClass
Attempt 4:
var url = window.content.location.href;
Error: Permission denied to access property 'href'
Attempt 5:
var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var currBrowser = currentWindow.getBrowser();
var url = currBrowser.currentURI.spec;
Error: Permission denied to get property XPCComponents.classes
Coding this for Chrome was a breeze. Not sure why this is so tough for FF.
Anyone got a solution?
Upvotes: 1
Views: 3584
Reputation: 21
I think you can use firefox local object:
var url = gBrowser.contentDocument.location;
Upvotes: 2
Reputation: 32063
I guess "Firefox Addon panel" refers to the Addon SDK's panel module?
If so you're probably trying to use those snippets in a content script. Instead you have to send an event to the main addon's code (example), and in the main addon's code use the tabs module:
require("tabs").activeTab.url
[update] complete testcase, which works for me:
// http://stackoverflow.com/questions/7856282/get-current-url-from-within-a-firefox-addon-panel
exports.main = function() {
var panel = require("panel").Panel({
contentURL: "data:text/html,<input type=button value='click me' id='b'>",
contentScript: "document.getElementById('b').onclick = " +
"function() {" +
" self.port.emit('myEvent');" +
"}"
});
panel.port.on("myEvent", function() {
console.log(require("tabs").activeTab.url)
})
require("widget").Widget({
id: "my-panel",
contentURL: "http://www.google.com/favicon.ico",
label: "Test Widget",
panel: panel
});
};
Upvotes: 1