single-file.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 args = require("yargs")
  26. .wrap(null)
  27. .command("$0 <url> [output]", "Save a page into a single HTML file.", yargs => {
  28. yargs.positional("url", { description: "URL of the page to save", type: "string" });
  29. yargs.positional("output", { description: "Output filename", type: "string" });
  30. })
  31. .default({
  32. "back-end": "puppeteer",
  33. "browser-headless": true,
  34. "browser-executable-path": "",
  35. "browser-width": 1280,
  36. "browser-height": 720,
  37. "browser-wait-until": "networkidle0",
  38. "compress-CSS": true,
  39. "compress-HTML": true,
  40. "enable-MAFF": false,
  41. "filename-template": "",
  42. "group-duplicate-images": true,
  43. "load-deferred-images": true,
  44. "load-deferred-images-max-idle-time": 1500,
  45. "max-resource-size-enabled": false,
  46. "max-resource-size": 10,
  47. "remove-hidden-elements": true,
  48. "remove-unused-styles": true,
  49. "remove-unused-fonts": true,
  50. "remove-frames": false,
  51. "remove-imports": true,
  52. "remove-scripts": true,
  53. "remove-audio-src": true,
  54. "remove-video-src": true,
  55. "remove-alternative-fonts": true,
  56. "remove-alternative-medias": true,
  57. "remove-alternative-images": true,
  58. "save-raw-page": false,
  59. "web-driver-executable-path": ""
  60. })
  61. .options("back-end", { description: "Back-end to use" })
  62. .choices("back-end", ["jsdom", "puppeteer", "webdriver-chromium", "webdriver-gecko"])
  63. .options("browser-headless", { description: "Run the browser in headless mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
  64. .boolean("browser-headless")
  65. .options("browser-executable-path", { description: "Path to chrome/chromium executable (puppeteer, webdriver-gecko, webdriver-chromium)" })
  66. .string("browser-executable-path")
  67. .options("browser-width", { description: "Width of the browser viewport in pixels" })
  68. .number("browser-width")
  69. .options("browser-height", { description: "Height of the browser viewport in pixels" })
  70. .number("browser-height")
  71. .options("browser-wait-until", { description: "When to consider the page is loaded (puppeteer, webdriver-gecko, webdriver-chromium)" })
  72. .choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
  73. .options("enable-MAFF", { description: "Enables support of MAFF pages with Firefox < 57 (webdriver-gecko)" })
  74. .boolean("enable-MAFF")
  75. .options("compress-CSS", { description: "Compress CSS stylesheets" })
  76. .boolean("compress-CSS")
  77. .options("compress-HTML", { description: "Compress HTML content" })
  78. .boolean("compress-HTML")
  79. .options("filename-template", { description: "Template used to generate the output filename (see help page of the extension for more info)" })
  80. .string("filename-template")
  81. .options("group-duplicate-images", { description: "Group duplicate images into CSS custom properties" })
  82. .boolean("compress-HTML")
  83. .options("load-deferred-images", { description: "Load deferred (a.k.a. lazy-loaded) images (puppeteer, webdriver-gecko, webdriver-chromium)" })
  84. .boolean("load-deferred-images")
  85. .options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images (puppeteer, webdriver-gecko, webdriver-chromium)" })
  86. .number("load-deferred-images")
  87. .options("max-resource-size-enabled", { description: "Enable removal of embedded resources exceeding a given size" })
  88. .boolean("max-resource-size-enabled")
  89. .options("max-resource-size", { description: "Maximum size of embedded resources (i.e. images, stylesheets, scripts and iframes)" })
  90. .number("max-resource-size")
  91. .options("remove-hidden-elements", { description: "Remove HTML elements which are not displayed" })
  92. .number("remove-hidden-elements")
  93. .options("remove-unused-styles", { description: "Remove unused CSS rules and unneeded declarations" })
  94. .number("remove-unused-styles")
  95. .options("remove-unused-fonts", { description: "Remove unused CSS font rules" })
  96. .number("remove-unused-fonts")
  97. .options("remove-frames", { description: "Remove frames (puppeteer, webdriver-gecko, webdriver-chromium)" })
  98. .number("remove-frames")
  99. .options("remove-imports", { description: "Remove HTML imports" })
  100. .number("remove-imports")
  101. .options("remove-scripts", { description: "Remove JavaScript scripts" })
  102. .number("remove-scripts")
  103. .options("remove-audio-src", { description: "Remove source of audio elements" })
  104. .number("remove-audio-src")
  105. .options("remove-video-src", { description: "Remove source of video elements" })
  106. .number("remove-video-src")
  107. .options("remove-alternative-fonts", { description: "Remove alternative fonts to the ones displayed" })
  108. .number("remove-alternative-fonts")
  109. .options("remove-alternative-medias", { description: "Remove alternative CSS stylesheets" })
  110. .number("remove-alternative-medias")
  111. .options("remove-alternative-images", { description: "Remove images for alternative sizes of screen" })
  112. .number("remove-alternative-images")
  113. .options("save-raw-page", { description: "Save the original page without interpreting it into the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
  114. .number("save-raw-page")
  115. .options("web-driver-executable-path", { description: "Path to Selenium WebDriver executable (webdriver-gecko, webdriver-chromium)" })
  116. .string("web-driver-executable-path")
  117. .argv;
  118. const backEnds = {
  119. jsdom: "./back-ends/jsdom.js",
  120. puppeteer: "./back-ends/puppeteer.js",
  121. "webdriver-chromium": "./back-ends/webdriver-chromium.js",
  122. "webdriver-gecko": "./back-ends/webdriver-gecko.js"
  123. };
  124. require(backEnds[args.backEnd]).getPageData(args).then(pageData => {
  125. if (args.output) {
  126. require("fs").writeFileSync(args.output, pageData.content);
  127. } else {
  128. if (args.filenameTemplate) {
  129. require("fs").writeFileSync(pageData.filename, pageData.content);
  130. } else {
  131. console.log(pageData.content); // eslint-disable-line no-console
  132. }
  133. }
  134. });