Jase Pellerin
Jase Pellerin

Reputation: 397

How can I get the html of a page as a string in a Firefox extension?

I need to get the html of the current page that is loaded as a string, so that I may manipulate it and use that information later on. I am needing to use this in a Firefox extension, and I am having a lot of trouble getting it to work.

I originally tried storing the value using .outerHTML, which I had seen and got to work in other places. Here is an example of how that worked:

var pageHTML = document.documentElement.outerHTML; 

I also tried searching just for the piece that I needed at the time, like so:

document.getElementById("header")

However, neither of these seem to access the HTML. I assume this is because the code is operating in the browser, not in the document itself. How can I go about accessing the HTML 'document' of a page loaded in a tab from a Firefox extension.

Upvotes: 0

Views: 120

Answers (1)

Chris Hayes
Chris Hayes

Reputation: 12020

The Firefox addon SDK includes a module called page-mod which is intended for this purpose. Content scripts run under page-mod will be run in the context of the web page, rather than the typical sandboxed context.

From that page's documentation:

You can modify the document in your script:

var pageMod = require("sdk/page-mod");

pageMod.PageMod({ include: "*.mozilla.org",
                  contentScript: 'document.body.innerHTML =' +
                                 '"<h1>Page matches ruleset</h1>";' 
});

Upvotes: 2

Related Questions