args.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global require, module */
  24. const args = require("yargs")
  25. .wrap(null)
  26. .command("$0 [url] [output]", "Save a page into a single HTML file.", yargs => {
  27. yargs.positional("url", { description: "URL or path on the filesystem of the page to save", type: "string" });
  28. yargs.positional("output", { description: "Output filename", type: "string" });
  29. })
  30. .default({
  31. "accept-headers": {
  32. "font": "application/font-woff2;q=1.0,application/font-woff;q=0.9,*/*;q=0.8",
  33. "image": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
  34. "stylesheet": "text/css,*/*;q=0.1",
  35. "script": "*/*",
  36. "document": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  37. },
  38. "back-end": "puppeteer",
  39. "block-mixed-content": false,
  40. "browser-server": "",
  41. "browser-headless": true,
  42. "browser-executable-path": "",
  43. "browser-width": 1280,
  44. "browser-height": 720,
  45. "browser-load-max-time": 60000,
  46. "browser-wait-delay": 0,
  47. "browser-wait-until": "networkidle0",
  48. "browser-wait-until-fallback": true,
  49. "browser-debug": false,
  50. "browser-script": [],
  51. "browser-stylesheet": [],
  52. "browser-args": "",
  53. "browser-start-minimized": false,
  54. "browser-cookie": [],
  55. "browser-cookies-file": "",
  56. "compress-CSS": false,
  57. "compress-HTML": true,
  58. "dump-content": false,
  59. "emulateMediaFeature": [],
  60. "filename-template": "{page-title} ({date-iso} {time-locale}).html",
  61. "filename-conflict-action": "uniquify",
  62. "filename-replacement-character": "_",
  63. "filename-max-length": 192,
  64. "filename-max-length-unit": "bytes",
  65. "group-duplicate-images": true,
  66. "http-header": [],
  67. "include-infobar": false,
  68. "insert-meta-csp": true,
  69. "load-deferred-images": true,
  70. "load-deferred-images-dispatch-scroll-event": false,
  71. "load-deferred-images-max-idle-time": 1500,
  72. "load-deferred-images-keep-zoom-level": false,
  73. "max-parallel-workers": 8,
  74. "max-resource-size-enabled": false,
  75. "max-resource-size": 10,
  76. "move-styles-in-head": false,
  77. "output-directory": "",
  78. "remove-hidden-elements": true,
  79. "remove-unused-styles": true,
  80. "remove-unused-fonts": true,
  81. "remove-frames": false,
  82. "remove-imports": true,
  83. "block-scripts": true,
  84. "block-audios": true,
  85. "block-videos": true,
  86. "remove-alternative-fonts": true,
  87. "remove-alternative-medias": true,
  88. "remove-alternative-images": true,
  89. "save-original-urls": false,
  90. "save-raw-page": false,
  91. "web-driver-executable-path": "",
  92. "user-script-enabled": true,
  93. "include-BOM": false,
  94. "crawl-links": false,
  95. "crawl-inner-links-only": true,
  96. "crawl-remove-url-fragment": true,
  97. "crawl-max-depth": 1,
  98. "crawl-external-links-max-depth": 1,
  99. "crawl-replace-urls": false,
  100. "crawl-rewrite-rule": []
  101. })
  102. .options("back-end", { description: "Back-end to use" })
  103. .choices("back-end", ["jsdom", "puppeteer", "webdriver-chromium", "webdriver-gecko", "puppeteer-firefox", "playwright-firefox", "playwright-chromium"])
  104. .options("block-mixed-content", { description: "Block mixed contents" })
  105. .boolean("block-mixed-content")
  106. .options("browser-server", { description: "Server to connect to (puppeteer only for now)" })
  107. .string("browser-server")
  108. .options("browser-headless", { description: "Run the browser in headless mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  109. .boolean("browser-headless")
  110. .options("browser-executable-path", { description: "Path to chrome/chromium executable (puppeteer, webdriver-gecko, webdriver-chromium)" })
  111. .string("browser-executable-path")
  112. .options("browser-width", { description: "Width of the browser viewport in pixels" })
  113. .number("browser-width")
  114. .options("browser-height", { description: "Height of the browser viewport in pixels" })
  115. .number("browser-height")
  116. .options("browser-load-max-time", { description: "Maximum delay of time to wait for page loading in ms (puppeteer, webdriver-gecko, webdriver-chromium)" })
  117. .number("browser-load-max-time")
  118. .options("browser-wait-delay", { description: "Time to wait before capturing the page in ms" })
  119. .number("browser-wait-delay")
  120. .options("browser-wait-until", { description: "When to consider the page is loaded (puppeteer, webdriver-gecko, webdriver-chromium)" })
  121. .choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
  122. .options("browser-wait-until-fallback", { description: "Retry with the next value of --browser-wait-until when a timeout error is thrown" })
  123. .boolean("browser-wait-until-fallback")
  124. .options("browser-debug", { description: "Enable debug mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  125. .boolean("browser-debug")
  126. .options("browser-script", { description: "Path of a script executed in the page (and all the frames) before it is loaded" })
  127. .array("browser-script")
  128. .options("browser-stylesheet", { description: "Path of a stylesheet file inserted into the page (and all the frames) after it is loaded" })
  129. .array("browser-stylesheet")
  130. .options("browser-args", { description: "Arguments provided as a JSON array and passed to the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  131. .string("browser-args")
  132. .options("browser-start-minimized", { description: "Minimize the browser (puppeteer)" })
  133. .boolean("browser-start-minimized")
  134. .options("browser-cookie", { description: "Ordered list of cookie parameters separated by a comma: name,value,domain,path,expires,httpOnly,secure,sameSite,url (puppeteer, webdriver-gecko, webdriver-chromium, jsdom)" })
  135. .array("browser-cookie")
  136. .options("browser-cookies-file", { description: "Path of the cookies file formatted as a JSON file or a Netscape text file (puppeteer, webdriver-gecko, webdriver-chromium, jsdom)" })
  137. .string("browser-cookies-file")
  138. .options("compress-CSS", { description: "Compress CSS stylesheets" })
  139. .boolean("compress-CSS")
  140. .options("compress-HTML", { description: "Compress HTML content" })
  141. .boolean("compress-HTML")
  142. .options("crawl-links", { description: "Crawl and save pages found via inner links" })
  143. .boolean("crawl-links")
  144. .options("crawl-inner-links-only", { description: "Crawl pages found via inner links only if they are hosted on the same domain" })
  145. .boolean("crawl-inner-links-only")
  146. .options("crawl-no-parent", { description: "Crawl pages found via inner links only if their URLs are not parent of the URL to crawl" })
  147. .boolean("crawl-no-parent")
  148. .options("crawl-load-session", { description: "Name of the file of the session to load (previously saved with --crawl-save-session or --crawl-sync-session)" })
  149. .string("crawl-load-session")
  150. .options("crawl-remove-url-fragment", { description: "Remove URL fragments found in links" })
  151. .boolean("crawl-remove-url-fragment")
  152. .options("crawl-save-session", { description: "Name of the file where to save the state of the session" })
  153. .string("crawl-save-session")
  154. .options("crawl-sync-session", { description: "Name of the file where to load and save the state of the session" })
  155. .string("crawl-sync-session")
  156. .options("crawl-max-depth", { description: "Max depth when crawling pages found in internal and external links (0: infinite)" })
  157. .number("crawl-max-depth")
  158. .options("crawl-external-links-max-depth", { description: "Max depth when crawling pages found in external links (0: infinite)" })
  159. .number("crawl-external-links-max-depth")
  160. .options("crawl-replace-urls", { description: "Replace URLs of saved pages with relative paths of saved pages on the filesystem" })
  161. .boolean("crawl-replace-urls")
  162. .options("crawl-rewrite-rule", { description: "Rewrite rule used to rewrite URLs of crawled pages" })
  163. .array("crawl-rewrite-rule")
  164. .options("dump-content", { description: "Dump the content of the processed page in the console ('true' when running in Docker)" })
  165. .boolean("dump-content")
  166. .options("emulate-media-feature", { description: "Emulate a media feature. The syntax is <name>:<value>, e.g. \"prefers-color-scheme:dark\" (puppeteer)" })
  167. .array("emulate-media-feature")
  168. .options("error-file")
  169. .string("error-file")
  170. .options("filename-template", { description: "Template used to generate the output filename (see help page of the extension for more info)" })
  171. .string("filename-template")
  172. .options("filename-conflict-action", { description: "Action when the filename is conflicting with existing one on the filesystem. The possible values are \"uniquify\" (default), \"overwrite\" and \"skip\"" })
  173. .string("filename-conflict-action")
  174. .options("filename-replacement-character", { description: "The character used for replacing invalid characters in filenames" })
  175. .string("filename-replacement-character")
  176. .options("filename-max-length", { description: "Specify the maximum length of the filename" })
  177. .number("filename-max-length")
  178. .options("filename-max-length-unit", { description: "Specify the unit of the maximum length of the filename ('bytes' or 'chars')" })
  179. .string("filename-max-length-unit")
  180. .options("group-duplicate-images", { description: "Group duplicate images into CSS custom properties" })
  181. .boolean("group-duplicate-images")
  182. .options("http-header", { description: "Extra HTTP header (puppeteer, jsdom)" })
  183. .array("http-header")
  184. .options("include-BOM", { description: "Include the UTF-8 BOM into the HTML page" })
  185. .boolean("include-BOM")
  186. .options("include-infobar", { description: "Include the infobar" })
  187. .boolean("include-infobar")
  188. .options("insert-meta-csp", { description: "Include a <meta> tag with a CSP to avoid potential requests to internet when viewing a page" })
  189. .boolean("insert-meta-csp")
  190. .options("load-deferred-images", { description: "Load deferred (a.k.a. lazy-loaded) images (puppeteer, webdriver-gecko, webdriver-chromium)" })
  191. .boolean("load-deferred-images")
  192. .options("load-deferred-images-dispatch-scroll-event", { description: "Dispatch 'scroll' event when loading deferred images" })
  193. .boolean("load-deferred-images-dispatch-scroll-event")
  194. .options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images in ms (puppeteer, webdriver-gecko, webdriver-chromium)" })
  195. .number("load-deferred-images-max-idle-time")
  196. .options("load-deferred-images-keep-zoom-level", { description: "Load deferred images by keeping zoomed out the page" })
  197. .boolean("load-deferred-images-keep-zoom-level")
  198. .options("max-parallel-workers", { description: "Maximum number of browsers launched in parallel when processing a list of URLs (cf --urls-file)" })
  199. .number("max-parallel-workers")
  200. .options("max-resource-size-enabled", { description: "Enable removal of embedded resources exceeding a given size" })
  201. .boolean("max-resource-size-enabled")
  202. .options("max-resource-size", { description: "Maximum size of embedded resources in MB (i.e. images, stylesheets, scripts and iframes)" })
  203. .number("max-resource-size")
  204. .options("move-styles-in-head", { description: "Move style elements outside the head element into the head element" })
  205. .boolean("move-styles-in-head")
  206. .options("remove-frames", { description: "Remove frames (puppeteer, webdriver-gecko, webdriver-chromium)" })
  207. .boolean("remove-frames")
  208. .options("remove-hidden-elements", { description: "Remove HTML elements which are not displayed" })
  209. .boolean("remove-hidden-elements")
  210. .options("remove-unused-styles", { description: "Remove unused CSS rules and unneeded declarations" })
  211. .boolean("remove-unused-styles")
  212. .options("remove-unused-fonts", { description: "Remove unused CSS font rules" })
  213. .boolean("remove-unused-fonts")
  214. .options("remove-imports", { description: "Remove HTML imports" })
  215. .boolean("remove-imports")
  216. .options("block-scripts", { description: "Block scripts" })
  217. .boolean("block-scripts")
  218. .options("block-audios", { description: "Block audio elements" })
  219. .boolean("block-audios")
  220. .options("block-videos", { description: "Block video elements" })
  221. .boolean("block-videos")
  222. .options("remove-alternative-fonts", { description: "Remove alternative fonts to the ones displayed" })
  223. .boolean("remove-alternative-fonts")
  224. .options("remove-alternative-medias", { description: "Remove alternative CSS stylesheets" })
  225. .boolean("remove-alternative-medias")
  226. .options("remove-alternative-images", { description: "Remove images for alternative sizes of screen" })
  227. .boolean("remove-alternative-images")
  228. .options("save-original-urls", { description: "Save the original URLS in the embedded contents" })
  229. .boolean("save-original-urls")
  230. .options("save-raw-page", { description: "Save the original page without interpreting it into the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  231. .boolean("save-raw-page")
  232. .options("urls-file", { description: "Path to a text file containing a list of URLs (separated by a newline) to save" })
  233. .string("urls-file")
  234. .options("user-agent", { description: "User-agent of the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  235. .string("user-agent")
  236. .options("user-script-enabled", { description: "Enable the event API allowing to execute scripts before the page is saved" })
  237. .boolean("user-script-enabled")
  238. .options("web-driver-executable-path", { description: "Path to Selenium WebDriver executable (webdriver-gecko, webdriver-chromium)" })
  239. .string("web-driver-executable-path")
  240. .options("output-directory", { description: "Path to where to save files, this path must exist." })
  241. .string("output-directory")
  242. .argv;
  243. args.backgroundSave = true;
  244. args.compressCSS = args.compressCss;
  245. args.compressHTML = args.compressHtml;
  246. args.includeBOM = args.includeBom;
  247. args.crawlReplaceURLs = args.crawlReplaceUrls;
  248. args.crawlRemoveURLFragment = args.crawlRemoveUrlFragment;
  249. args.insertMetaCSP = args.insertMetaCsp;
  250. if (args.removeScripts) {
  251. args.blockScripts = true;
  252. }
  253. if (args.removeAudioSrc) {
  254. args.blockAudios = true;
  255. }
  256. if (args.removeVideoSrc) {
  257. args.blockVideos = true;
  258. }
  259. const headers = args.httpHeader;
  260. delete args.httpHeader;
  261. args.httpHeaders = {};
  262. headers.forEach(header => {
  263. const matchedHeader = header.match(/^(.*?):(.*)$/);
  264. if (matchedHeader.length == 3) {
  265. args.httpHeaders[matchedHeader[1].trim()] = matchedHeader[2].trimLeft();
  266. }
  267. });
  268. const cookies = args.browserCookie;
  269. delete args.browserCookie;
  270. args.browserCookies = cookies.map(cookieValue => {
  271. const value = cookieValue.split(/(?<!\\),/);
  272. return {
  273. name: value[0],
  274. value: value[1],
  275. domain: value[2] || undefined,
  276. path: value[3] || undefined,
  277. expires: value[4] && Number(value[4]) || undefined,
  278. httpOnly: value[5] && value[5] == "true" || undefined,
  279. secure: value[6] && value[5] == "true" || undefined,
  280. sameSite: value[7] || undefined,
  281. url: value[8] || undefined
  282. };
  283. });
  284. args.browserScripts = args.browserScript;
  285. delete args.browserScript;
  286. args.browserStylesheets = args.browserStylesheet;
  287. delete args.browserStylesheet;
  288. args.crawlRewriteRules = args.crawlRewriteRule;
  289. delete args.crawlRewriteRule;
  290. args.emulateMediaFeatures = args.emulateMediaFeature
  291. .map(value => {
  292. const splitValue = value.match(/^([^:]+):(.*)$/);
  293. if (splitValue.length >= 3) {
  294. return { name: splitValue[1].trim(), value: splitValue[2].trim() };
  295. }
  296. })
  297. .filter(identity => identity);
  298. delete args.emulateMediaFeature;
  299. Object.keys(args).filter(optionName => optionName.includes("-"))
  300. .forEach(optionName => delete args[optionName]);
  301. delete args["$0"];
  302. delete args["_"];
  303. module.exports = args;