singlefile_companion.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/local/bin/node
  2. /*
  3. * Copyright 2010-2020 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, process */
  25. const fs = require("fs");
  26. const path = require("path");
  27. const nativeMessage = require("./lib/messaging.js");
  28. const backEnds = {
  29. jsdom: "./../cli/back-ends/jsdom.js",
  30. puppeteer: "./../cli/back-ends/puppeteer.js",
  31. "puppeteer-firefox": "./../cli/back-ends/puppeteer-firefox.js",
  32. "webdriver-chromium": "./../cli/back-ends/webdriver-chromium.js",
  33. "webdriver-gecko": "./../cli/back-ends/webdriver-gecko.js"
  34. };
  35. process.stdin
  36. .pipe(new nativeMessage.Input())
  37. .pipe(new nativeMessage.Transform(function (msg, push, done) {
  38. capturePage(msg);
  39. push(JSON.stringify(msg));
  40. done();
  41. }))
  42. .pipe(new nativeMessage.Output())
  43. .pipe(process.stdout);
  44. async function capturePage(options) {
  45. const companionOptions = require("./options.json");
  46. const backend = require(backEnds[companionOptions.backEnd || "puppeteer"]);
  47. await backend.initialize(companionOptions);
  48. try {
  49. const pageData = await backend.getPageData(options);
  50. pageData.filename = path.resolve("../../", (companionOptions.savePath || ""), pageData.filename);
  51. fs.writeFileSync(getFilename(pageData.filename), pageData.content);
  52. return pageData;
  53. } catch (error) {
  54. if (companionOptions.errorFile) {
  55. const message = "URL: " + options.url + "\nStack: " + error.stack + "\n";
  56. fs.writeFileSync(options.errorFile, message, { flag: "a" });
  57. }
  58. } finally {
  59. await backend.closeBrowser();
  60. process.exit(0);
  61. }
  62. }
  63. function getFilename(filename, index = 1) {
  64. let newFilename = filename;
  65. if (index > 1) {
  66. const regExpMatchExtension = /(\.[^.]+)$/;
  67. const matchExtension = newFilename.match(regExpMatchExtension);
  68. if (matchExtension && matchExtension[1]) {
  69. newFilename = newFilename.replace(regExpMatchExtension, " - " + index + matchExtension[1]);
  70. } else {
  71. newFilename += " - " + index;
  72. }
  73. }
  74. if (fs.existsSync(newFilename)) {
  75. return getFilename(filename, index + 1);
  76. } else {
  77. return newFilename;
  78. }
  79. }