简体   繁体   中英

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.

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>";' }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM