runner.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.runner = (() => {
  22. const contentScriptFiles = [
  23. "/lib/hooks/hooks.js",
  24. "/lib/browser-polyfill/chrome-browser-polyfill.js",
  25. "/lib/single-file/vendor/css-tree.js",
  26. "/lib/single-file/vendor/html-srcset-parser.js",
  27. "/lib/single-file/util/timeout.js",
  28. "/lib/single-file/util/doc-helper.js",
  29. "/lib/fetch/content/fetch.js",
  30. "/lib/lazy/content/content-lazy-loader.js",
  31. "/lib/single-file/single-file-core.js",
  32. "/lib/single-file/single-file-browser.js",
  33. "/extension/index.js",
  34. "/extension/ui/content/content-ui.js",
  35. "/extension/core/content/content.js"
  36. ];
  37. const frameScriptFiles = [
  38. "/lib/hooks/hooks-frame.js",
  39. "/lib/browser-polyfill/chrome-browser-polyfill.js",
  40. "/extension/index.js",
  41. "/lib/single-file/util/doc-helper.js",
  42. "/lib/single-file/util/timeout.js",
  43. "/lib/fetch/content/fetch.js",
  44. "/lib/lazy/content/content-lazy-loader.js",
  45. "/lib/frame-tree/frame-tree.js",
  46. "/extension/core/content/content-frame.js"
  47. ];
  48. const optionalContentScriptFiles = {
  49. compressHTML: [
  50. "/lib/single-file/modules/html-minifier.js",
  51. "/lib/single-file/modules/html-serializer.js"
  52. ],
  53. compressCSS: [
  54. "/lib/single-file/vendor/css-minifier.js"
  55. ],
  56. removeAlternativeFonts: [
  57. "/lib/single-file/modules/css-fonts-alt-minifier.js"
  58. ],
  59. removeAlternativeMedias: [
  60. "/lib/single-file/vendor/css-media-query-parser.js",
  61. "/lib/single-file/modules/css-medias-alt-minifier.js"
  62. ],
  63. removeAlternativeImages: [
  64. "/lib/single-file/modules/html-images-alt-minifier.js"
  65. ],
  66. removeUnusedFonts: [
  67. "/lib/single-file/vendor/css-font-property-parser.js",
  68. "/lib/single-file/modules/css-fonts-minifier.js"
  69. ],
  70. removeUnusedStyles: [
  71. "/lib/single-file/modules/css-matched-rules.js",
  72. "/lib/single-file/modules/css-rules-minifier.js"
  73. ]
  74. };
  75. return { saveTab };
  76. async function saveTab(tab, options) {
  77. if (!options.removeFrames) {
  78. await executeContentScripts(tab.id, frameScriptFiles, true, "document_start");
  79. }
  80. await executeContentScripts(tab.id, getContentScriptFiles(options), false, "document_idle");
  81. if (options.frameId) {
  82. await browser.tabs.sendMessage(tab.id, { saveFrame: true, options }, { frameId: options.frameId });
  83. } else {
  84. await browser.tabs.sendMessage(tab.id, { savePage: true, options });
  85. }
  86. }
  87. async function executeContentScripts(tabId, scriptFiles, allFrames, runAt) {
  88. for (const file of scriptFiles) {
  89. await browser.tabs.executeScript(tabId, { file, allFrames, runAt });
  90. }
  91. }
  92. function getContentScriptFiles(options) {
  93. let files = contentScriptFiles;
  94. Object.keys(optionalContentScriptFiles).forEach(option => {
  95. if (options[option]) {
  96. files = optionalContentScriptFiles[option].concat(files);
  97. }
  98. });
  99. return files;
  100. }
  101. })();