1
0

scripts.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global require, exports */
  24. const fs = require("fs");
  25. const SCRIPTS = [
  26. "common/ui/content/content-infobar.js"
  27. ];
  28. const INDEX_SCRIPTS = [
  29. "lib/single-file/dist/single-file.js",
  30. "common/index.js"
  31. ];
  32. const WEB_SCRIPTS = [
  33. "/lib/single-file/processors/hooks/content/content-hooks-web.js",
  34. "/lib/single-file/processors/hooks/content/content-hooks-frames-web.js",
  35. "/common/ui/content/content-infobar-web.js"
  36. ];
  37. exports.get = async options => {
  38. const basePath = "../../../";
  39. let scripts = await readScriptFiles(INDEX_SCRIPTS, basePath);
  40. const webScripts = {};
  41. await Promise.all(WEB_SCRIPTS.map(async path => webScripts[path] = await readScriptFile(path, basePath)));
  42. scripts += "this.singlefile.getFileContent = filename => (" + JSON.stringify(webScripts) + ")[filename];\n";
  43. scripts += await readScriptFiles(SCRIPTS, basePath);
  44. scripts += await readScriptFiles(options && options.browserScripts ? options.browserScripts : [], "");
  45. if (options.browserStylesheets && options.browserStylesheets.length) {
  46. scripts += "addEventListener(\"load\",()=>{const styleElement=document.createElement(\"style\");styleElement.textContent=" + JSON.stringify(await readScriptFiles(options.browserStylesheets, "")) + ";document.body.appendChild(styleElement);});";
  47. }
  48. return scripts;
  49. };
  50. async function readScriptFiles(paths, basePath = "../../../") {
  51. return (await Promise.all(paths.map(path => readScriptFile(path, basePath)))).join("");
  52. }
  53. function readScriptFile(path, basePath) {
  54. return new Promise((resolve, reject) =>
  55. fs.readFile(basePath ? require.resolve(basePath + path) : path, (err, data) => {
  56. if (err) {
  57. reject(err);
  58. } else {
  59. resolve(data.toString() + "\n");
  60. }
  61. })
  62. );
  63. }