1
0

args.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. "back-end": "puppeteer",
  32. "browser-server": "",
  33. "browser-headless": true,
  34. "browser-executable-path": "",
  35. "browser-width": 1280,
  36. "browser-height": 720,
  37. "browser-load-max-time": 60000,
  38. "browser-wait-delay": 0,
  39. "browser-wait-until": "networkidle0",
  40. "browser-wait-until-fallback": true,
  41. "browser-debug": false,
  42. "browser-script": [],
  43. "browser-stylesheet": [],
  44. "browser-args": "",
  45. "browser-start-minimized": false,
  46. "browser-cookie": [],
  47. "browser-cookies-file": "",
  48. "compress-CSS": false,
  49. "compress-HTML": true,
  50. "dump-content": false,
  51. "filename-template": "{page-title} ({date-iso} {time-locale}).html",
  52. "filename-conflict-action": "uniquify",
  53. "filename-replacement-character": "_",
  54. "group-duplicate-images": true,
  55. "http-header": [],
  56. "include-infobar": false,
  57. "load-deferred-images": true,
  58. "load-deferred-images-max-idle-time": 1500,
  59. "load-deferred-images-keep-zoom-level": false,
  60. "maxParallelWorkers": 8,
  61. "max-resource-size-enabled": false,
  62. "max-resource-size": 10,
  63. "remove-hidden-elements": true,
  64. "remove-unused-styles": true,
  65. "remove-unused-fonts": true,
  66. "remove-frames": false,
  67. "remove-imports": true,
  68. "remove-scripts": true,
  69. "remove-audio-src": true,
  70. "remove-video-src": true,
  71. "remove-alternative-fonts": true,
  72. "remove-alternative-medias": true,
  73. "remove-alternative-images": true,
  74. "save-raw-page": false,
  75. "web-driver-executable-path": "",
  76. "user-script-enabled": true,
  77. "include-BOM": false,
  78. "crawl-links": false,
  79. "crawl-inner-links-only": true,
  80. "crawl-remove-url-fragment": true,
  81. "crawl-max-depth": 1,
  82. "crawl-external-links-max-depth": 1,
  83. "crawl-replace-urls": false,
  84. "crawl-rewrite-rule": [],
  85. "output-directory": ""
  86. })
  87. .options("back-end", { description: "Back-end to use" })
  88. .choices("back-end", ["jsdom", "puppeteer", "webdriver-chromium", "webdriver-gecko", "puppeteer-firefox", "playwright-firefox", "playwright-chromium"])
  89. .options("browser-server", { description: "Server to connect to (puppeteer only for now)" })
  90. .string("browser-server")
  91. .options("browser-headless", { description: "Run the browser in headless mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  92. .boolean("browser-headless")
  93. .options("browser-executable-path", { description: "Path to chrome/chromium executable (puppeteer, webdriver-gecko, webdriver-chromium)" })
  94. .string("browser-executable-path")
  95. .options("browser-width", { description: "Width of the browser viewport in pixels" })
  96. .number("browser-width")
  97. .options("browser-height", { description: "Height of the browser viewport in pixels" })
  98. .number("browser-height")
  99. .options("browser-load-max-time", { description: "Maximum delay of time to wait for page loading in ms (puppeteer, webdriver-gecko, webdriver-chromium)" })
  100. .number("browser-load-max-time")
  101. .options("browser-wait-delay", { description: "Time to wait before capturing the page in ms" })
  102. .number("browser-wait-delay")
  103. .options("browser-wait-until", { description: "When to consider the page is loaded (puppeteer, webdriver-gecko, webdriver-chromium)" })
  104. .choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
  105. .options("browser-wait-until-fallback", { description: "Retry with the next value of --browser-wait-until when a timeout error is thrown" })
  106. .boolean("browser-wait-until-fallback")
  107. .options("browser-debug", { description: "Enable debug mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  108. .boolean("browser-debug")
  109. .options("browser-script", { description: "Path of a script executed in the page (and all the frames) before it is loaded" })
  110. .array("browser-script")
  111. .options("browser-stylesheet", { description: "Path of a stylesheet file inserted into the page (and all the frames) after it is loaded" })
  112. .array("browser-stylesheet")
  113. .options("browser-args", { description: "Arguments provided as a JSON array and passed to the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  114. .string("browser-args")
  115. .options("browser-start-minimized", { description: "Minimize the browser (puppeteer)" })
  116. .boolean("browser-start-minimized")
  117. .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)" })
  118. .array("browser-cookie")
  119. .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)" })
  120. .string("browser-cookies-file")
  121. .options("compress-CSS", { description: "Compress CSS stylesheets" })
  122. .boolean("compress-CSS")
  123. .options("compress-HTML", { description: "Compress HTML content" })
  124. .boolean("compress-HTML")
  125. .options("crawl-links", { description: "Crawl and save pages found via inner links" })
  126. .boolean("crawl-links")
  127. .options("crawl-inner-links-only", { description: "Crawl pages found via inner links only if they are hosted on the same domain" })
  128. .boolean("crawl-inner-links-only")
  129. .options("crawl-no-parent", { description: "Crawl pages found via inner links only if their URLs are not parent of the URL to crawl" })
  130. .boolean("crawl-no-parent")
  131. .options("crawl-load-session", { description: "Name of the file of the session to load (previously saved with --crawl-save-session or --crawl-sync-session)" })
  132. .string("crawl-load-session")
  133. .options("crawl-remove-url-fragment", { description: "Remove URL fragments found in links" })
  134. .boolean("crawl-remove-url-fragment")
  135. .options("crawl-save-session", { description: "Name of the file where to save the state of the session" })
  136. .string("crawl-save-session")
  137. .options("crawl-sync-session", { description: "Name of the file where to load and save the state of the session" })
  138. .string("crawl-sync-session")
  139. .options("crawl-max-depth", { description: "Max depth when crawling pages found in internal and external links (0: infinite)" })
  140. .number("crawl-max-depth")
  141. .options("crawl-external-links-max-depth", { description: "Max depth when crawling pages found in external links (0: infinite)" })
  142. .number("crawl-external-links-max-depth")
  143. .options("crawl-replace-urls", { description: "Replace URLs of saved pages with relative paths of saved pages on the filesystem" })
  144. .boolean("crawl-replace-urls")
  145. .options("crawl-rewrite-rule", { description: "Rewrite rule used to rewrite URLs of crawled pages" })
  146. .array("crawl-rewrite-rule")
  147. .options("dump-content", { description: "Dump the content of the processed page in the console" })
  148. .boolean("dump-content")
  149. .options("error-file")
  150. .string("error-file")
  151. .options("filename-template", { description: "Template used to generate the output filename (see help page of the extension for more info)" })
  152. .string("filename-template")
  153. .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\"" })
  154. .string("filename-conflict-action")
  155. .options("filename-replacement-character", { description: "The character used for replacing invalid characters in filenames" })
  156. .string("filename-replacement-character")
  157. .string("filename-replacement-character")
  158. .options("group-duplicate-images", { description: "Group duplicate images into CSS custom properties" })
  159. .boolean("group-duplicate-images")
  160. .options("http-header", { description: "Extra HTTP header (puppeteer, jsdom)" })
  161. .array("http-header")
  162. .options("include-BOM", { description: "Include the UTF-8 BOM into the HTML page" })
  163. .boolean("include-BOM")
  164. .options("include-infobar", { description: "Include the infobar" })
  165. .boolean("include-infobar")
  166. .options("load-deferred-images", { description: "Load deferred (a.k.a. lazy-loaded) images (puppeteer, webdriver-gecko, webdriver-chromium)" })
  167. .boolean("load-deferred-images")
  168. .options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images in ms (puppeteer, webdriver-gecko, webdriver-chromium)" })
  169. .number("load-deferred-images-max-idle-time")
  170. .options("load-deferred-images-keep-zoom-level", { description: "Load defrrred images by keeping zoomed out the page" })
  171. .boolean("load-deferred-images-keep-zoom-level")
  172. .options("max-parallel-workers", { description: "Maximum number of browsers launched in parallel when processing a list of URLs (cf --urls-file)" })
  173. .number("max-parallel-workers")
  174. .options("max-resource-size-enabled", { description: "Enable removal of embedded resources exceeding a given size" })
  175. .boolean("max-resource-size-enabled")
  176. .options("max-resource-size", { description: "Maximum size of embedded resources in MB (i.e. images, stylesheets, scripts and iframes)" })
  177. .number("max-resource-size")
  178. .options("remove-frames", { description: "Remove frames (puppeteer, webdriver-gecko, webdriver-chromium)" })
  179. .boolean("remove-frames")
  180. .options("remove-hidden-elements", { description: "Remove HTML elements which are not displayed" })
  181. .boolean("remove-hidden-elements")
  182. .options("remove-unused-styles", { description: "Remove unused CSS rules and unneeded declarations" })
  183. .boolean("remove-unused-styles")
  184. .options("remove-unused-fonts", { description: "Remove unused CSS font rules" })
  185. .boolean("remove-unused-fonts")
  186. .options("remove-imports", { description: "Remove HTML imports" })
  187. .boolean("remove-imports")
  188. .options("remove-scripts", { description: "Remove JavaScript scripts" })
  189. .boolean("remove-scripts")
  190. .options("remove-audio-src", { description: "Remove source of audio elements" })
  191. .boolean("remove-audio-src")
  192. .options("remove-video-src", { description: "Remove source of video elements" })
  193. .boolean("remove-video-src")
  194. .options("remove-alternative-fonts", { description: "Remove alternative fonts to the ones displayed" })
  195. .boolean("remove-alternative-fonts")
  196. .options("remove-alternative-medias", { description: "Remove alternative CSS stylesheets" })
  197. .boolean("remove-alternative-medias")
  198. .options("remove-alternative-images", { description: "Remove images for alternative sizes of screen" })
  199. .boolean("remove-alternative-images")
  200. .options("save-raw-page", { description: "Save the original page without interpreting it into the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  201. .boolean("save-raw-page")
  202. .options("urls-file", { description: "Path to a text file containing a list of URLs (separated by a newline) to save" })
  203. .string("urls-file")
  204. .options("user-agent", { description: "User-agent of the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  205. .string("user-agent")
  206. .options("user-script-enabled", { description: "Enable the event API allowing to execute scripts before the page is saved" })
  207. .boolean("user-script-enabled")
  208. .options("web-driver-executable-path", { description: "Path to Selenium WebDriver executable (webdriver-gecko, webdriver-chromium)" })
  209. .string("web-driver-executable-path")
  210. .options("output-directory", { description: "Path to where to save files, this path must exist." })
  211. .string("output-directory")
  212. .argv;
  213. args.backgroundSave = true;
  214. args.compressCSS = args.compressCss;
  215. args.compressHTML = args.compressHtml;
  216. args.includeBOM = args.includeBom;
  217. args.crawlReplaceURLs = args.crawlReplaceUrls;
  218. args.crawlRemoveURLFragment = args.crawlRemoveUrlFragment;
  219. const headers = args.httpHeader;
  220. delete args.httpHeader;
  221. args.httpHeaders = {};
  222. headers.forEach(header => {
  223. const matchedHeader = header.match(/^(.*?):(.*)$/);
  224. if (matchedHeader.length == 3) {
  225. args.httpHeaders[matchedHeader[1].trim()] = matchedHeader[2].trimLeft();
  226. }
  227. });
  228. const cookies = args.browserCookie;
  229. delete args.browserCookie;
  230. args.browserCookies = cookies.map(cookieValue => {
  231. const value = cookieValue.split(/(?<!\\),/);
  232. return {
  233. name: value[0],
  234. value: value[1],
  235. domain: value[2] || undefined,
  236. path: value[3] || undefined,
  237. expires: value[4] && Number(value[4]) || undefined,
  238. httpOnly: value[5] && value[5] == "true" || undefined,
  239. secure: value[6] && value[5] == "true" || undefined,
  240. sameSite: value[7] || undefined,
  241. url: value[8] || undefined
  242. };
  243. });
  244. args.browserScripts = args.browserScript;
  245. delete args.browserScript;
  246. args.browserStylesheets = args.browserStylesheet;
  247. delete args.browserStylesheet;
  248. args.crawlRewriteRules = args.crawlRewriteRule;
  249. delete args.crawlRewriteRule;
  250. Object.keys(args).filter(optionName => optionName.includes("-"))
  251. .forEach(optionName => delete args[optionName]);
  252. delete args["$0"];
  253. delete args["_"];
  254. module.exports = args;