Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find URL of current tab. Making a FireFox Browser add-on

I'm making a Firefox Browser Add-on and need to find the url of the current tab

I've tried this post Opening a URL in current tab/window from a Firefox Extension but it tells me that 'window' is not defined. (I think because I am making an add-on rather than an extension.)

Here's what I've tried to do:

var widgets = require('widget');
var tabs = require('tabs');

var widget1 = widgets.Widget({
  id: "widget1",
  label: "widget1",
  contentURL: "http://www.mozilla.org/favicon",
  onClick: function() {
    console.log(tabs.url);
  }
})

I've made a widget such that when I click it the url of the current tab should be 'console.log'ed.

Doesn't seem to happen! Keep getting "info: undefined" which clearly means that tabs.url isn't returning anything. But this seems to be the way to use it according to https://addons.mozilla.org/en-US/developers/docs/sdk/1.5/packages/addon-kit/docs/tabs.html

Anyone have any ideas?

Thanks,

Will

like image 948
WillJones Avatar asked Jul 20 '12 15:07

WillJones


People also ask

How do I find my URL in Firefox?

If you want to get current URL in current Firefox extension, just add content script (developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/…) and ask directly for document. location from it.

What is the URL of new tab in Firefox?

You can look at this extension if you use the home page as the new tab page. *New Tab Homepage: https://addons.mozilla.org/firefox/addon/new-tab-homepage/ Classic Theme Restorer includes this feature as well. You find "New tab page url" in Tabs page.

How do I copy the URL address of all open tabs in Firefox?

More videos on YouTube Once it's safe, click the use Current Pages button to add the URL from every tab into the Home Page box. Click inside the field and press Ctrl + A to select all the text, then Ctrl + C to copy it. Then you can paste it into Notepad or Word to have a list of all URLs.


1 Answers

You're almost there:

const { ActionButton } = require("sdk/ui/button/action");
const clipboard = require("sdk/clipboard");
const tabs = require('sdk/tabs');

let button = ActionButton({
  id: "my-button-id",
  label: "Button Label",
  icon: {
    "32": "chrome://mozapps/skin/extensions/extensionGeneric.png"
  },
  onClick: function(state) {
    let url = tabs.activeTab.url;
    console.log("active tab url:", url);
    require("sdk/notifications").notify({
      title: "Active Tab's Url is "+url,
      text: "Click to copy.",
      onClick: function() {
        clipboard.set(url);
      }
    });
  }
});

You should check out the documentation on the tabs module.

Note: I've updated this code example to use the new ui modules available since Firefox 29 - the 'widget' module used in the original question was valid at the time but has since been deprecated and then removed.

like image 172
therealjeffg Avatar answered Sep 20 '22 13:09

therealjeffg