singlefile_companion.js 2.6 KB

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