1
0

single-file 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env 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 */
  25. const fileUrl = require("file-url");
  26. const fs = require("fs");
  27. const api = require("./single-file-cli-api");
  28. run(require("./args"))
  29. .catch(error => console.error(error.message || error)); // eslint-disable-line no-console
  30. async function run(options) {
  31. let urls;
  32. if (options.url && !api.VALID_URL_TEST.test(options.url)) {
  33. options.url = fileUrl(options.url);
  34. }
  35. if (options.urlsFile) {
  36. urls = fs.readFileSync(options.urlsFile).toString().split("\n");
  37. } else {
  38. urls = [options.url];
  39. }
  40. if (options.browserCookiesFile) {
  41. const cookiesContent = fs.readFileSync(options.browserCookiesFile).toString();
  42. try {
  43. options.browserCookies = JSON.parse(cookiesContent);
  44. } catch (error) {
  45. options.browserCookies = parseCookies(cookiesContent);
  46. }
  47. }
  48. options.retrieveLinks = true;
  49. const singlefile = await api.initialize(options);
  50. await singlefile.capture(urls);
  51. await singlefile.finish();
  52. }
  53. function parseCookies(textValue) {
  54. const httpOnlyRegExp = /^#HttpOnly_(.*)/;
  55. return textValue.split(/\r\n|\n/)
  56. .filter(line => line.trim() && (!/^#/.test(line) || httpOnlyRegExp.test(line)))
  57. .map(line => {
  58. const httpOnly = httpOnlyRegExp.test(line);
  59. if (httpOnly) {
  60. line = line.replace(httpOnlyRegExp, "$1");
  61. }
  62. const values = line.split(/\t/);
  63. if (values.length == 7) {
  64. return {
  65. domain: values[0],
  66. path: values[2],
  67. secure: values[3] == "TRUE",
  68. expires: (values[4] && Number(values[4])) || undefined,
  69. name: values[5],
  70. value: values[6],
  71. httpOnly
  72. };
  73. }
  74. })
  75. .filter(cookieData => cookieData);
  76. }