script-loader.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global browser, singlefile */
  21. singlefile.scriptLoader = (() => {
  22. const contentScriptFiles = [
  23. "/lib/browser-polyfill/custom-browser-polyfill.js",
  24. "/lib/single-file/doc-helper.js",
  25. "/lib/single-file/base64.js",
  26. "/lib/single-file/css-srcset-parser.js",
  27. "/lib/fetch/content/fetch.js",
  28. "/lib/single-file/single-file-core.js",
  29. "/lib/single-file/single-file-browser.js",
  30. "/extension/index.js",
  31. "/extension/ui/content/content-ui.js",
  32. "/extension/core/content/content.js"
  33. ];
  34. const frameScriptFiles = [
  35. "/lib/browser-polyfill/custom-browser-polyfill.js",
  36. "/extension/index.js",
  37. "/lib/single-file/doc-helper.js",
  38. "/lib/single-file/timeout.js",
  39. "/lib/single-file/frame-tree.js",
  40. "/extension/core/content/content-frame.js"
  41. ];
  42. const optionalContentScriptFiles = {
  43. compressHTML: [
  44. "/lib/single-file/html-minifier.js",
  45. "/lib/single-file/html-serializer.js"
  46. ],
  47. compressCSS: [
  48. "/lib/single-file/css-uglifycss.js"
  49. ],
  50. removeAlternativeFonts: [
  51. "/lib/single-file/css-fonts-minifier.js"
  52. ],
  53. removeUnusedStyles: [
  54. "/lib/single-file/css-what.js",
  55. "/lib/single-file/css-declarations-parser.js",
  56. "/lib/single-file/css-rules-matcher.js",
  57. "/lib/single-file/css-minifier.js"
  58. ],
  59. lazyLoadImages: [
  60. "/lib/single-file/lazy-loader.js",
  61. ]
  62. };
  63. return { executeScripts };
  64. async function executeScripts(tab, options) {
  65. if (!options.removeFrames) {
  66. await executeContentScripts(tab.id, frameScriptFiles, true);
  67. }
  68. await executeContentScripts(tab.id, getContentScriptFiles(options), false);
  69. if (options.frameId) {
  70. await browser.tabs.sendMessage(tab.id, { saveFrame: true, options }, { frameId: options.frameId });
  71. } else {
  72. await browser.tabs.sendMessage(tab.id, { savePage: true, options });
  73. }
  74. }
  75. async function executeContentScripts(tabId, scriptFiles, allFrames) {
  76. return Promise.all(scriptFiles.map(file => browser.tabs.executeScript(tabId, { file, allFrames })));
  77. }
  78. function getContentScriptFiles(options) {
  79. let files = contentScriptFiles;
  80. Object.keys(optionalContentScriptFiles).forEach(option => {
  81. if (options[option]) {
  82. files = optionalContentScriptFiles[option].concat(files);
  83. }
  84. });
  85. return files;
  86. }
  87. })();