簡體   English   中英

以單個 html 文件格式保存當前網頁

[英]Save the current webpage in a single html file format

I am working on a project where at some point I want to add the functionality of saving the current webpage as HTML, also in pdf, I am saving page in pdf format by this javascript function window.print() now I want to save it作為HTML,但找不到辦法做到這一點,我希望有一些function保存為HTML。 如果你知道,請告訴我,這將是一個很大的幫助。 請注意,您可以通過鍵盤快捷鍵 Ctrl+S 將當前網頁另存為 Html,並使用鍵盤快捷鍵 Ctrl+P 打印頁面。

使用document.querySelector("html").innerHTML獲取一個頁面的所有 HTML。

然后你有一個包含整個文檔作為字符串的變量 - 我們可以使用以下 function 下載它:

function download(filename, text) {
 var element = document.createElement('a');
 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + 
 encodeURIComponent(text));
 element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

資源

也看看這個 SO 線程

暫無
暫無

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

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