As you are writing an add-on to Firefox, you should know that Firefox itself is a great program written in Javascript and XUL (its weird markup language, which serves, in a way, as a native-faced HTML with many more components). Well, when you click the extension button, the current document is the Firefox window itself, so it will display the XUL source code from the window.
I assume your Javascript code is something like the below:
var seesource = {
run : function(aEvent) {
alert(document.documentElement.outerHTML);
}
};
Fortunately, the scope of scripts will contain a variable called content
, that will reference the contents of the current tab. The secret is to get the document
of this variable:
var seesource = {
run : function(aEvent) {
alert(content.document.documentElement.outerHTML);
}
};
See the result
This, however, is not valid if you are using the Addon SDK, the most modern way to develop complements. In this case, the thing will be a little more complicated (in English) but clearly not your case.
Thus?
alert(document.documentElement.outerHTML);
– user622
@Gabrielsantos, I tried this way, but on all the pages it shows the same code, see the print: http://prntscr.com/3ph5la pq will be ? has some lock on firefox ?
– user7605
You want the original source code? (type, what the server sent via HTTP, disregarding any changes to the page made via Javascript for example) Or the current content of the DOM? And is this Javascript code on the page itself, or elsewhere? (browser extension, bookmarklet...)
– mgibsonbr
@mgibsonbr, I want to see the original source code of the current page opened in the tab. For example, ta open there.com globe, I want to see the source code of that page. This java script code I put in a button on the FIREFOX taskbar.
– user7605
Why don’t you use CTRL+U? The code will rarely fit in one
alert
.– bfavaretto
@bfavaretto, it’s okay not to fit in an ALERT, it can be in a console.log, then I’ll put the code in a variable, understand ? ALERT was just an example.
– user7605
Like this
alert
will be shown? Will it appear when you click a button, as in http://jsfiddle.net/brandizzi/sQ498/? It will always run, as in http://jsfiddle.net/brandizzi/GfRDZ/? Is it on a bookmarklet? In all these scenarios, the @Gabrielsantos solution worked here. Show us the code you already has and is not working.– brandizzi
By the way, here is another question: what do you want to do with the source code put in a variable? Why do you want to recover it all? Maybe there are better ways to do what you want.
– brandizzi
@brandizzi, really this way your worked by accessing the page by firefox. In my case I created a button in the firefox toolbar written "View Code". When I click on this button should display the code understood ? I created an extension for this...
– user7605
@brandizzi, from what I understand here, it seems that he not only takes the html code but even the toolbar because the button is there, you know ? at least it seems that’s it....
– user7605
@brandizzi, it’s expensive, that’s right...as my button stays on the taskbar and I click to display the code, it’s taking the codes from the TABS and not the HTML page is soft ?
– user7605
So the code is in an extension/add-on/addon, right? What is the version of your Firefox?
– brandizzi
That’s right @brandizzi, is in an addon/add-on, my version is the 29.0.1, I believe it is the latest version....
– user7605
Aaaaaaaaaanow it makes sense :) I have drafted an extension here and I think I have found the answer. A suggestion: edit the question, to make it clear that you are writing Javascript for an add-on, which is not Javascript on an HTML page itself. There is a world of difference between complements and pages :)
– brandizzi