Marcos Eusebi
Marcos Eusebi

Reputation: 637

Firefox add-on get the tab body content

Hello everyone i have an question about firefox add-on:

How i can get the body content from a tab, for example.

var content = require("tabs").activeTab.documentContent.body.innerHTML;

Thanks alot.

Upvotes: 1

Views: 3598

Answers (3)

user1742529
user1742529

Reputation: 264

You can try this:

var tabs = require("sdk/tabs");
var { getTabForId, getTabContentWindow } = require ("sdk/tabs/utils");
var tab = require("tabs").activeTab;
var window = getTabContentWindow (getTabForId(tab.id));
var content = window.document.body.innerHTML;

But maybe this answer is better.

Upvotes: 1

Wayne
Wayne

Reputation: 60424

You can get the body of the currently-selected tab using the following (after DOMContentLoaded):

gBrowser.contentDocument.body.innerHTML

Note: This only works in a standard extension, not in the SDK.

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57681

The Add-on SDK doesn't allow direct access to the tab contents - the idea is that the add-on and the tab might end up living in different processes eventually. What you do is injecting a content script into the tab to get you the necessary data, something like this:

var tab = require("tabs").activeTab;
tab.attach({
  contentScript: "self.postMessage(document.body.innerHTML);",
  onMessage: function(data)
  {
    console.log("Tab data received: " + data);
  }
});

Upvotes: 3

Related Questions