single-file-cli.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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": "chrome",
  35. "compress-CSS": true,
  36. "compress-HTML": true,
  37. "group-duplicate-images": true,
  38. "load-deferred-images": true,
  39. "load-deferred-images-max-idle-time": 1500,
  40. "max-resource-size-enabled": false,
  41. "max-resource-size": 10,
  42. "remove-hidden-elements": true,
  43. "remove-unused-styles": true,
  44. "remove-unused-fonts": true,
  45. "remove-frames": false,
  46. "remove-imports": true,
  47. "remove-scripts": true,
  48. "remove-audio-src": true,
  49. "remove-video-src": true,
  50. "remove-alternative-fonts": true,
  51. "remove-alternative-medias": true,
  52. "remove-alternative-images": true,
  53. "save-raw-page": false
  54. })
  55. .options("back-end", { description: "Back-end to use" })
  56. .choices("back-end", ["jsdom", "puppeteer", "webdriver"])
  57. .options("browser-headless", { description: "Run the browser in headless mode" })
  58. .boolean("browser-headless")
  59. .options("browser-executable-path", { description: "Path to chrome/chromium executable" })
  60. .string("browser-executable-path")
  61. .options("compress-CSS", { description: "Compress CSS stylesheets" })
  62. .boolean("compress-CSS")
  63. .options("compress-HTML", { description: "Compress HTML content" })
  64. .boolean("compress-HTML")
  65. .options("group-duplicate-images", { description: "Group duplicate images into CSS custom properties" })
  66. .boolean("compress-HTML")
  67. .options("load-deferred-images", { description: "Load deferred (aka lazy-loaded) images" })
  68. .boolean("load-deferred-images")
  69. .options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images" })
  70. .number("load-deferred-images")
  71. .options("max-resource-size-enabled", { description: "Enable removal of embedded resources exceeding a given size" })
  72. .boolean("max-resource-size-enabled")
  73. .options("max-resource-size", { description: "Maximum size of embedded resources (i.e. images, stylesheets, scripts and iframes)" })
  74. .number("max-resource-size")
  75. .options("remove-hidden-elements", { description: "Remove HTML elements which are not displayed" })
  76. .number("remove-hidden-elements")
  77. .options("remove-unused-styles", { description: "Remove unused CSS rules and unneeded declarations" })
  78. .number("remove-unused-styles")
  79. .options("remove-unused-fonts", { description: "Remove unused CSS font rules" })
  80. .number("remove-unused-fonts")
  81. .options("remove-frames", { description: "Remove frames" })
  82. .number("remove-frames")
  83. .options("remove-imports", { description: "Remove HTML imports" })
  84. .number("remove-imports")
  85. .options("remove-scripts", { description: "Remove JavaScript scripts" })
  86. .number("remove-scripts")
  87. .options("remove-audio-src", { description: "Remove source of audio elements" })
  88. .number("remove-audio-src")
  89. .options("remove-video-src", { description: "Remove source of video elements" })
  90. .number("remove-video-src")
  91. .options("remove-alternative-fonts", { description: "Remove alternative fonts to the ones displayed" })
  92. .number("remove-alternative-fonts")
  93. .options("remove-alternative-medias", { description: "Remove alternative CSS stylesheets" })
  94. .number("remove-alternative-medias")
  95. .options("remove-alternative-images", { description: "Remove images for alternative sizes of screen" })
  96. .number("remove-alternative-images")
  97. .options("save-raw-page", { description: "Save the original page without interpreting it into the browser" })
  98. .number("save-raw-page")
  99. .argv;
  100. const backEnds = {
  101. jsdom: "./single-file-jsdom.js",
  102. puppeteer: "./single-file-puppeteer.js",
  103. webdriver: "./single-file-webdriver.js"
  104. };
  105. require(backEnds[args.backEnd]).getPageData(args).then(pageData => {
  106. if (args.output) {
  107. require("fs").writeFileSync(args.output, pageData.content);
  108. } else {
  109. console.log(pageData.content); // eslint-disable-line no-console
  110. }
  111. });