用浏览器自带的网页另存功能时,经常出现丢失图片,而且还会保存一堆的关联文件。最近 GitHub 上有一个热门开源工具,可以完美解决这些问题。

​简介

SingleFile 是一个浏览器扩展,以及 CLI 工具,可快速将完整的网页保存成单一 HTML 文件。

保存网页时“丢三落四”?8k Star 的开源扩展,一键完美保存完整网页  网页 工具 开源 第1张

它兼容 Chrome、Firefox(桌面和移动端)、Edge、Vivaldi、Brave、Waterfox、Yandex 和 Opera 等主流浏览器。

项目地址:

https://github.com/gildas-lormeau/SingleFile

安装

SingleFile 可以安装在:

  • Firefox: https://extensionworkshop.com/documentation/develop/temporary-installation-in-firefox/
  • Chrome: https://developer.chrome.com/extensions/getstarted#manifest
  • Microsoft Edge: https://docs.microsoft.com/en-us/microsoft-edge/extensions-chromium/getting-started/part1-simple-extension#run-your-extension-locally-in-your-browser-while-developing-it-side-loading
  • Firefox: https://addons.mozilla.org/firefox/addon/single-file
  • Firefox 移动端:https://blog.mozilla.org/addons/2020/09/29/expanded-extension-support-in-firefox-for-android-nightly/
  • Chrome: https://chrome.google.com/extensions/detail/mpiodijhokgodhhofbcjdecpffjipkle
  • Microsoft Edge: https://microsoftedge.microsoft.com/addons/detail/efnbkdcfmcmnhlkaijjjmhjjgladedno
  • 也可以通过手动下载 zip 文件,解压到磁盘上并且按照以下说明手动安装:https://github.com/gildas-lormeau/SingleFile/archive/master.zip

简单使用

等到页面完全加载后,单击扩展工具栏中的 SingleFile 按钮以保存页面,在处理页面时再次单击该按钮以取消该操作。

  • 当前 tab 的内容
  • 选中的内容
  • 选中的 frame
  • 通过右键单击扩展工具栏或网页上的 SingleFile 按钮打开菜单,可以保存:
  • 也可以一键处理多个 tab 并保存:
  • 选中的 tab
  • 未固定的 tab
  • 所有的 tab
  • 在菜单中选择 "Annotate and save the page...":
  • 可以高亮文本
  • 添加注释
  • 删除内容
  • 如果启用自动保存,页面每次加载后都会自动保存页面
  • 文件下载后保存路径是浏览器配置中的下载文件夹

SingleFile的命令行界面

SingleFile 可以通过命令行启动,它通过 Node.js 作为注入网页的独立脚本运行。

使用 Docker 安装

  • 从 Docker Hub 安装
dockerpullcapsulecode/singlefile
dockertagcapsulecode/singlefilesinglefile
  • 手动安装
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile/cli
dockerbuild--no-cache-tsinglefile.

  • 运行
dockerrunsinglefile"https://www.wikipedia.org"

  • 运行并将结果重定向到文件中
dockerrunsinglefile"https://www.wikipedia.org">wikipedia.html

手动安装

全局下载和安装

  • 确保已经安装了 Chrome 或 Firefox,并且可以通过 PATH 环境变量找到可执行文件
  • 安装 Node.js
  • 下载安装 SingleFile 有以下 3 种方法:
npminstall-g"gildas-lormeau/SingleFile#master"
  • 手动下载并解压
unzipmaster.zip.
cdSingleFile-master
npminstall
cdcli
  • git 源码安装
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile
npminstall
cdcli

运行

  • 语法:
single-file<url>[output][options...]
  • 查看帮助:
single-file--help

保存页面内容到指定文件

  • 例子
single-filehttps://www.wikipedia.orgwikipedia.html
  • 保存 list-urls.txt 文件中的 url 列表
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile/cli
dockerbuild--no-cache-tsinglefile.

0

与用户脚本集成

可以在 SingleFile 保存页面之前或之后执行用户脚本。

  1. 当 SignleFile 作为:
  • 扩展使用时,从选项页面导出设置、编辑 JSON 文件、替换 userScriptEnabled: false 为 userScriptEnabled: true,并在 SingleFile 中导入修改后的文件来启用隐藏选项。
  • CLI 工具使用时,使用选项 --browser-script 将脚本路径传递给 SingleFile。
  1. 在用户脚本中分发自定义事件:
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile/cli
dockerbuild--no-cache-tsinglefile.

1
  1. 在用户脚本中监听自定义事件 single-file-on-before-capture-request,这个监听函数会在页面保存前被调用:
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile/cli
dockerbuild--no-cache-tsinglefile.

2
  1. 在用户脚本中监听自定义事件 single-file-on-after-capture-request,这个监听函数会在页面保存后被调用:
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile/cli
dockerbuild--no-cache-tsinglefile.

3
  1. 例子,这个脚本会在保存页面之前从页面中删除图像,并在处理页面后恢复:
(()=>{
constelements=newMap();
constremovedElementsSelector="img";
gitclone--depth1--recursivehttps://github.com/gildas-lormeau/SingleFile.git
cdSingleFile/cli
dockerbuild--no-cache-tsinglefile.

1
addEventListener("single-file-on-before-capture-request",()=>{
document.querySelectorAll(removedElementsSelector).forEach(element=>{
constplaceHolderElement=document.createElement(element.tagName);
elements.set(placeHolderElement,element);
element.parentElement.replaceChild(placeHolderElement,element);
});
});

addEventListener("single-file-on-after-capture-request",()=>{
Array.from(elements).forEach(([placeHolderElement,element])=>{
placeHolderElement.parentElement.replaceChild(element,placeHolderElement);
});
elements.clear();
});
})();

转载请说明出处
知优网 » 保存网页时“丢三落四”?8k Star 的开源扩展,一键完美保存完整网页

发表评论

您需要后才能发表评论