Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

336
Visualizações
How can I get the HTML of a site as a variable in Javascript or jQuery?

I'm trying to get 10 million digits of pi from this site to use as a variable in Javascript. I'd like to get the HTML from the site with a simple command instead of hardcoding it. I'm using Codepen so I can't just copy and paste the text (Codepen doesn't allow so many characters). Can you help me get the HTML?

Any help is greatly appreciated!

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Copying and pasting those digits would take hours

Shouldn't it be as simple as ctrl+a then copy? Then just trim the top and bottom unnecessary parts? Anyway here's a solution you could try.


  1. Open the website
  2. Open the Developer Tools [ Ctrl+Shift+i ], I'm on Google Chrome on a linux build
  3. Copy & Paste the code below into the console
  4. Profit, 10MB file

saveToFile(document.querySelector('center').textContent);

function saveToFile(string){
    var blob = new Blob([string], {type:'application/text'});
    var url = URL.createObjectURL(blob);

    var hiddenAnchorElem = document.createElement('a');
    applyAttrs.call(hiddenAnchorElem, {
        href: url,
        target: '_blank',
        download: 'pi.text'
    });

    hiddenAnchorElem.click();

    function applyAttrs( attrs ){
        var keys = Object.keys;

        keys(attrs).forEach( attr => {
            this[attr] = attrs[attr];
        });
    }
}
about 4 years ago · Santiago Trujillo Relatório

0

You could use AJAX, if it's on the same server as your website, or if CORS is allowed by the site (which is unfortunately not likely).

You'll probably want to write server side code that pulls in the HTML of the page you would like to request, and prints it out - a proxy of sorts - and use the URL of that page rather than the actual page you are trying to get the HTML for.

Here's an example of how AJAX can be used: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest. In that example, the HTML of the page would be stored in this.responseText in the callback handler for the AJAX call's onload event.

If you're just using this locally, though, you may be able to get around the Cross Origin Request Policy.

If you run Chrome from your Command Prompt using:

Chrome.exe --disable-web-security

...from the directory in which Chrome is installed, you'll be able to use AJAX to get the contents of the page without any server-side code.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda