简体   繁体   中英

How to save a complete page in Chrome using JavaScript (from Dev Tools)?

In Chrome, to save a complete web page I can click Ctrl+S > "Format: Web page, Single File" > save it as ".mhtml".

How can I do the same thing but automatically, using JavaScript?

If I open the chrome devtools, is there a line of javascript I can enter that will accomplish that?

just give this pice of code into your console (Chrome Dev Tools) and the popup where to save (The webpage will be saved as an HTML file in your default download location) will open

(async () => {
  const html = await fetch(window.location.href).then(response => response.text());
  const blob = new Blob([html], { type: 'text/html' });
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.style.display = 'none';
  a.href = url;
  a.download = 'page.html';
  document.body.appendChild(a);
  a.click();
  setTimeout(() => {
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
  }, 100);
})();

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