簡體   English   中英

如何使用 JavaScript(來自 Dev Tools)在 Chrome 中保存完整頁面?

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

在 Chrome 中,要保存完整的網頁,我可以單擊 Ctrl+S >“格式:網頁,單個文件”> 將其另存為“.mhtml”。

我怎樣才能使用 JavaScript 自動執行相同的操作?

如果我打開 chrome devtools,我可以輸入一行 javascript 來完成嗎?

只需將這段代碼輸入您的控制台(Chrome Dev Tools),然后彈出保存位置(該網頁將在您的默認下載位置保存為 HTML 文件)將打開

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM