Chris
Chris

Reputation: 59491

Sending current tab's URL to plugin content

I'm making a add-on for Firefox using the Add-on SDK and I need to somehow get the active tabs URL to the add-on's content, whenever the tab is created or switched to.

My main.js looks like this:

var { ToggleButton } = require('sdk/ui/button/toggle');
var panels = require("sdk/panel");
var self = require("sdk/self");    

var button = ToggleButton({
  id: "my-button",
  label: "my button",
  icon: {
    "16": "./img/icon-16.png",
    "32": "./img/icon-32.png",
    "64": "./img/icon-64.png"
  },
  onChange: handleChange
});

var panel = panels.Panel({
  width: 640,
  height: 400,
  contentURL: self.data.url("index.html"),
  onHide: handleHide
});

function handleChange(state) {
  if (state.checked) {
    panel.show({
      position: button
    });
  }
}

function handleHide() {
  button.state('window', {checked: false});
}

The index.html that's loaded into the panel has some embedded JavaScript and that's where I need to send the current URL to.

I've looked at the documentation here but it speaks of "content-scripts", something I'm unsure of if I even use, or need to use.

So which is the quickest way of doing this? Thanks.

Upvotes: 1

Views: 81

Answers (1)

erikvold
erikvold

Reputation: 16508

You'll need to use the sdk/tabs module:

var tabs = require("sdk/tabs");

var activeTabURL = tabs.activeTab.url;

tabs.on("activate", tab => {
  activeTabURL = tab.url;
});

tabs.on("ready", tab => {
  if (tab == tabs.activeTab) {
    activeTabURL = tab.url;
  }
});

With this the activeTabURL variable will contain the url of the active tab.

Upvotes: 2

Related Questions