Chris Kohout
Chris Kohout

Reputation: 95

Get current URL from within a Firefox Addon panel

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:

Coding this for Chrome was a breeze. Not sure why this is so tough for FF.

Anyone got a solution?

Upvotes: 1

Views: 3584

Answers (2)

user2122946
user2122946

Reputation: 21

I think you can use firefox local object:

var url = gBrowser.contentDocument.location;

Upvotes: 2

Nickolay
Nickolay

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

Related Questions