How to recover Firefox bi HTML source code via Addon/Add-on?

Asked

Viewed 527 times

3

I am writing an add-on ("addon") to Firefox. I need to get the HTML source code open in the current Firefox tab and display in a alert, using Javascript.

I tried to use alert(document.documentElement.outerHTML), but just shows the code below:

inserir a descrição da imagem aqui

Any suggestions ?

  • Thus? alert(document.documentElement.outerHTML);

  • @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 ?

  • 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, 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.

  • 1

    Why don’t you use CTRL+U? The code will rarely fit in one alert.

  • @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.

  • 1

    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.

  • 2

    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, 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...

  • @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....

  • @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 ?

  • So the code is in an extension/add-on/addon, right? What is the version of your Firefox?

  • That’s right @brandizzi, is in an addon/add-on, my version is the 29.0.1, I believe it is the latest version....

  • 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 :)

Show 9 more comments

2 answers

4


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

alert() mostrando o código-fonte HTML de pudim.com.br

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.

  • Caramba @brandizzi, perfect your answer, just needed to put this blessed content kkkkkkkkkkk .... Thanks friend, I will edit the question yes. Hugs!!!

1

If the page has jQuery included, the following works:

$.get(document.html, function(html) { alert(html); } );

But to work on all pages, the ideal is to use the pure Javascript version:

// No IE, só funciona a partir da versão 9

request = new XMLHttpRequest();
request.open("GET", document.url, true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400){
    // Sucesso!
    html = request.responseText;
    alert(html);
  } else {
    alert("O servidor retornou um erro")
  }
};

request.onerror = function() {
  alert("Não foi possível conectar ao servidor");
};

request.send();

You can copy and paste to the console on any page to test.

Tip: When in doubt how to do something without jQuery, see: You Might Not Need jQuery

  • I put this code in my js file, it does nothing...it does not appear any screen or anything rsrsrs... You even tested it ?

  • @user7605 Yes here worked. Did you try the second code? Gives some output (error) on the console?

  • this appears on the console: NS_ERROR_FILE_NOT_FOUND:

  • see: http://prntscr.com/3pld42

  • @user7605 Had tested on Chrome and it works. I tested on Firefox now and it launches one of the error messages if you take the comment (// Sucesso).

  • didn’t understand @Andrey..... withdraws that comment ?

  • There’s a comment in the middle of the code (// Sucesso) that makes Firefox lose, because the code is all in one line on the console. Removing it solved the build error, but it is returning server error.

  • Really @Andrey, of the same error, has some more idea ?

  • The right thing would be location.hrefand not document.url

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.