Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

335
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda