Vivek Goel
Vivek Goel

Reputation: 24160

Getting page title in Firefox add-on using Add-on SDK

I am trying to get page title on every page using new Firefox add-on builder. How can I do that?

Edit More info I want to get page title on every page load event .

Upvotes: 0

Views: 863

Answers (2)

Nandu
Nandu

Reputation: 808

"ready" events won't get fired if the page is served from back-forward cache. 'pageshow' event is the appropriate event to listen to.

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

function onOpen(tab) {
tab.on('pageshow', function(tab) {
           console.log('title: '+ tab.title);
  }

tabs.on('open', onOpen);

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57681

It is actually the very first example for the tabs package:

var tabs = require("tabs");
for each (var tab in tabs)
  console.log(tab.title);

See tab.title.

Edit: If you need to know the title of each page as it loads rather than capture the current state then you should use the page-mod package:

var pageMod = require("page-mod");
pageMod.PageMod({
  include: "*",
  contentScriptWhen: "end",
  contentScript: 'console.log(document.title);'
});

The documentation has some info on how a content script can communicate with the add-on, e.g. to send it this page title.

If you are only interested in top-level documents then you can still use the tabs package:

var tabs = require("tabs");
tabs.on("ready", function(tab) {
  console.log(tab.title);
});

Upvotes: 5

Related Questions