简体   繁体   中英

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!

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];
        });
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM