Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox extensions & XUL: get page source code

I am developing my first Firefox extension and for that I need to get the complete source code of the current page. How can I do that with XUL?

like image 746
Franz Avatar asked Feb 25 '10 21:02

Franz


2 Answers

You will need a xul browser object to load the content into.

Load the "view-source:" version of your page into a the browser object, in the same way as the "View Page Source" menu does. See function viewSource() in chrome://global/content/viewSource.js. That function can load from cache, or not.

Once the content is loaded, the original source is given by:

var source = browser.contentDocument.getElementById('viewsource').textContent;

Serialize a DOM Document
This method will not get the original source, but may be useful to some readers.

You can serialize the document object to a string. See Serializing DOM trees to strings in the MDC. You may need to use the alternate method of instantiation in your extension.

That article talks about XML documents, but it also works on any HTML DOMDocument.

var serializer = new XMLSerializer();
var source = serializer.serializeToString(document);

This even works in a web page or the firebug console.

like image 183
Lachlan Roche Avatar answered Oct 12 '22 10:10

Lachlan Roche


really looks like there is no way to get "all the sourcecode". You may use

document.documentElement.innerHTML

to get the innerHTML of the top element (usually html). If you have a php error message like

<h3>fatal error</h3>
segfault

<html>
    <head>
        <title>bla</title>
        <script type="text/javascript">
            alert(document.documentElement.innerHTML);
        </script>
    </head>
    <body>
    </body>
</html>

the innerHTML would be

<head>
<title>bla</title></head><body><h3>fatal error</h3>
segfault    
        <script type="text/javascript">
            alert(document.documentElement.innerHTML);
        </script></body>

but the error message would still retain

edit: documentElement is described here: https://developer.mozilla.org/en/DOM/document.documentElement

like image 32
Phil Rykoff Avatar answered Oct 12 '22 09:10

Phil Rykoff