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\").activ
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.
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.
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);
}
});