single-file 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env node
  2. /*
  3. * Copyright 2010-2019 Gildas Lormeau
  4. * contact : gildas.lormeau <at> gmail.com
  5. *
  6. * This file is part of SingleFile.
  7. *
  8. * The code in this file is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  11. * of the License, or (at your option) any later version.
  12. *
  13. * The code in this file is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  16. * General Public License for more details.
  17. *
  18. * As additional permission under GNU AGPL version 3 section 7, you may
  19. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  20. * AGPL normally required by section 4, provided you include this license
  21. * notice and a URL through which recipients can access the Corresponding
  22. * Source.
  23. */
  24. /* global require */
  25. const fileUrl = require("file-url");
  26. const args = require("yargs")
  27. .wrap(null)
  28. .command("$0 <url> [output]", "Save a page into a single HTML file.", yargs => {
  29. yargs.positional("url", { description: "URL or path on the filesystem of the page to save", type: "string" });
  30. yargs.positional("output", { description: "Output filename", type: "string" });
  31. })
  32. .default({
  33. "back-end": "puppeteer",
  34. "browser-headless": true,
  35. "browser-executable-path": "",
  36. "browser-width": 1280,
  37. "browser-height": 720,
  38. "browser-load-max-time": 60000,
  39. "browser-wait-until": "networkidle0",
  40. "browser-debug": false,
  41. "browser-extensions": [],
  42. "browser-scripts": [],
  43. "compress-CSS": true,
  44. "compress-HTML": true,
  45. "filename-template": "",
  46. "group-duplicate-images": true,
  47. "load-deferred-images": true,
  48. "load-deferred-images-max-idle-time": 1500,
  49. "max-resource-size-enabled": false,
  50. "max-resource-size": 10,
  51. "remove-hidden-elements": true,
  52. "remove-unused-styles": true,
  53. "remove-unused-fonts": true,
  54. "remove-frames": false,
  55. "remove-imports": true,
  56. "remove-scripts": true,
  57. "remove-audio-src": true,
  58. "remove-video-src": true,
  59. "remove-alternative-fonts": true,
  60. "remove-alternative-medias": true,
  61. "remove-alternative-images": true,
  62. "save-raw-page": false,
  63. "web-driver-executable-path": ""
  64. })
  65. .options("back-end", { description: "Back-end to use" })
  66. .choices("back-end", ["jsdom", "puppeteer", "webdriver-chromium", "webdriver-gecko"])
  67. .options("browser-headless", { description: "Run the browser in headless mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  68. .boolean("browser-headless")
  69. .options("browser-executable-path", { description: "Path to chrome/chromium executable (puppeteer, webdriver-gecko, webdriver-chromium)" })
  70. .string("browser-executable-path")
  71. .options("browser-width", { description: "Width of the browser viewport in pixels" })
  72. .number("browser-width")
  73. .options("browser-height", { description: "Height of the browser viewport in pixels" })
  74. .number("browser-height")
  75. .options("browser-load-max-time", { description: "Maximum delay of time to wait for page loading in ms (webdriver-gecko, webdriver-chromium)" })
  76. .number("browser-load-max-time")
  77. .options("browser-wait-until", { description: "When to consider the page is loaded (puppeteer, webdriver-gecko, webdriver-chromium)" })
  78. .choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
  79. .options("browser-debug", { description: "Enable debug mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  80. .boolean("browser-debug")
  81. .options("browser-extensions", { description: "List of extension paths separated by a space and relative to the 'cli' folder (webdriver-gecko, webdriver-chromium)" })
  82. .array("browser-extensions")
  83. .options("browser-scripts", { description: "List of script paths separated by a space and relative to the 'cli' folder. They will be executed in all the frames." })
  84. .array("browser-scripts")
  85. .options("compress-CSS", { description: "Compress CSS stylesheets" })
  86. .boolean("compress-CSS")
  87. .options("compress-HTML", { description: "Compress HTML content" })
  88. .boolean("compress-HTML")
  89. .options("filename-template", { description: "Template used to generate the output filename (see help page of the extension for more info)" })
  90. .string("filename-template")
  91. .options("group-duplicate-images", { description: "Group duplicate images into CSS custom properties" })
  92. .boolean("group-duplicate-images")
  93. .options("load-deferred-images", { description: "Load deferred (a.k.a. lazy-loaded) images (puppeteer, webdriver-gecko, webdriver-chromium)" })
  94. .boolean("load-deferred-images")
  95. .options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images in ms (puppeteer, webdriver-gecko, webdriver-chromium)" })
  96. .number("load-deferred-images-max-idle-time")
  97. .options("max-resource-size-enabled", { description: "Enable removal of embedded resources exceeding a given size" })
  98. .boolean("max-resource-size-enabled")
  99. .options("max-resource-size", { description: "Maximum size of embedded resources in MB (i.e. images, stylesheets, scripts and iframes)" })
  100. .number("max-resource-size")
  101. .options("remove-frames", { description: "Remove frames (puppeteer, webdriver-gecko, webdriver-chromium)" })
  102. .boolean("remove-frames")
  103. .options("remove-hidden-elements", { description: "Remove HTML elements which are not displayed" })
  104. .number("remove-hidden-elements")
  105. .options("remove-unused-styles", { description: "Remove unused CSS rules and unneeded declarations" })
  106. .number("remove-unused-styles")
  107. .options("remove-unused-fonts", { description: "Remove unused CSS font rules" })
  108. .number("remove-unused-fonts")
  109. .options("remove-imports", { description: "Remove HTML imports" })
  110. .number("remove-imports")
  111. .options("remove-scripts", { description: "Remove JavaScript scripts" })
  112. .number("remove-scripts")
  113. .options("remove-audio-src", { description: "Remove source of audio elements" })
  114. .number("remove-audio-src")
  115. .options("remove-video-src", { description: "Remove source of video elements" })
  116. .number("remove-video-src")
  117. .options("remove-alternative-fonts", { description: "Remove alternative fonts to the ones displayed" })
  118. .number("remove-alternative-fonts")
  119. .options("remove-alternative-medias", { description: "Remove alternative CSS stylesheets" })
  120. .number("remove-alternative-medias")
  121. .options("remove-alternative-images", { description: "Remove images for alternative sizes of screen" })
  122. .number("remove-alternative-images")
  123. .options("save-raw-page", { description: "Save the original page without interpreting it into the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  124. .number("save-raw-page")
  125. .options("web-driver-executable-path", { description: "Path to Selenium WebDriver executable (webdriver-gecko, webdriver-chromium)" })
  126. .string("web-driver-executable-path")
  127. .argv;
  128. const backEnds = {
  129. jsdom: "./back-ends/jsdom.js",
  130. puppeteer: "./back-ends/puppeteer.js",
  131. "webdriver-chromium": "./back-ends/webdriver-chromium.js",
  132. "webdriver-gecko": "./back-ends/webdriver-gecko.js"
  133. };
  134. args.compressCSS = args.compressCss;
  135. args.compressHTML = args.compressHtml;
  136. if (!/^(https?|file):\/\//.test(args.url)) {
  137. args.url = fileUrl(args.url);
  138. }
  139. args.browserScripts = args.browserScripts.map(path => require.resolve(path));
  140. require(backEnds[args.backEnd]).getPageData(args).then(pageData => {
  141. if (args.output) {
  142. require("fs").writeFileSync(args.output, pageData.content);
  143. } else {
  144. if (args.filenameTemplate) {
  145. require("fs").writeFileSync(pageData.filename, pageData.content);
  146. } else {
  147. console.log(pageData.content); // eslint-disable-line no-console
  148. }
  149. }
  150. });