core.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2010-2019 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, fetch, TextDecoder */
  21. singlefile.core = (() => {
  22. let contentScript, frameScript, optionalContentScript;
  23. const contentScriptFiles = [
  24. "/lib/hooks/hooks.js",
  25. "/lib/browser-polyfill/chrome-browser-polyfill.js",
  26. "/lib/single-file/vendor/css-tree.js",
  27. "/lib/single-file/vendor/html-srcset-parser.js",
  28. "/lib/single-file/util/timeout.js",
  29. "/lib/single-file/util/doc-helper.js",
  30. "/lib/single-file/util/doc-util.js",
  31. "/lib/fetch/content/fetch.js",
  32. "/lib/single-file/single-file-core.js",
  33. "/lib/single-file/single-file-browser.js",
  34. "/extension/index.js",
  35. "/extension/ui/content/content-ui.js",
  36. "/extension/core/content/content.js"
  37. ];
  38. const frameScriptFiles = [
  39. "/lib/hooks/hooks-frame.js",
  40. "/lib/browser-polyfill/chrome-browser-polyfill.js",
  41. "/extension/index.js",
  42. "/lib/single-file/util/doc-helper.js",
  43. "/lib/single-file/util/timeout.js",
  44. "/lib/fetch/content/fetch.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. loadDeferredImages: [
  57. "/lib/lazy/content/content-lazy-loader.js",
  58. function () { this.lazyLoader.getScriptPath = path => browser.runtime.getURL(path); }
  59. ],
  60. removeAlternativeImages: [
  61. "/lib/single-file/modules/html-images-alt-minifier.js"
  62. ],
  63. removeUnusedFonts: [
  64. "/lib/single-file/vendor/css-font-property-parser.js",
  65. "/lib/single-file/modules/css-fonts-minifier.js"
  66. ],
  67. removeAlternativeFonts: [
  68. "/lib/single-file/modules/css-fonts-alt-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. removeAlternativeMedias: [
  75. "/lib/single-file/vendor/css-media-query-parser.js",
  76. "/lib/single-file/modules/css-medias-alt-minifier.js"
  77. ]
  78. };
  79. initScripts();
  80. return { saveTab };
  81. async function saveTab(tab, options = {}) {
  82. if (singlefile.util.isAllowedURL(tab.url)) {
  83. await initScripts();
  84. const tabId = tab.id;
  85. options.tabId = tabId;
  86. try {
  87. singlefile.ui.button.onInitialize(tabId, options, 1);
  88. if (options.autoSave) {
  89. const options = await singlefile.config.getOptions(tab.url, true);
  90. if (singlefile.autosave.isEnabled(tab)) {
  91. await singlefile.tabs.sendMessage(tab.id, { autoSavePage: true, options });
  92. }
  93. } else {
  94. const mergedOptions = await singlefile.config.getOptions(tab.url);
  95. Object.keys(options).forEach(key => mergedOptions[key] = options[key]);
  96. if (!mergedOptions.removeFrames) {
  97. await browser.tabs.executeScript(tab.id, { code: frameScript, allFrames: true, runAt: "document_start" });
  98. }
  99. const code = await getContentScript(mergedOptions);
  100. await browser.tabs.executeScript(tab.id, { code, allFrames: false, runAt: "document_idle" });
  101. if (mergedOptions.frameId) {
  102. await singlefile.tabs.sendMessage(tab.id, { saveFrame: true, options: mergedOptions }, { frameId: mergedOptions.frameId });
  103. } else {
  104. await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
  105. }
  106. }
  107. singlefile.ui.button.onInitialize(tabId, options, 2);
  108. } catch (error) {
  109. console.log(error); // eslint-disable-line no-console
  110. singlefile.ui.button.onError(tabId, options);
  111. }
  112. }
  113. }
  114. async function initScripts() {
  115. if (!contentScript && !frameScript && !optionalContentScript) {
  116. optionalContentScript = {};
  117. [contentScript, frameScript] = await Promise.all([
  118. getScript(contentScriptFiles),
  119. getScript(frameScriptFiles),
  120. Promise.all(Object.keys(optionalContentScriptFiles)
  121. .map(async option => optionalContentScript[option] = await getScript(optionalContentScriptFiles[option])))
  122. ]);
  123. }
  124. }
  125. async function getContentScript(options) {
  126. await initScripts();
  127. let script = "";
  128. Object.keys(optionalContentScriptFiles).forEach(option => {
  129. if (options[option]) {
  130. script += optionalContentScript[option] + "\n";
  131. }
  132. });
  133. return script + "\n" + contentScript;
  134. }
  135. async function getScript(scriptFiles) {
  136. const scriptsPromises = scriptFiles.map(async scriptFile => {
  137. if (typeof scriptFile == "function") {
  138. return "(" + scriptFile.toString() + ")();";
  139. } else {
  140. const scriptResource = await fetch(browser.runtime.getURL(scriptFile));
  141. return new TextDecoder().decode(await scriptResource.arrayBuffer());
  142. }
  143. });
  144. let content = "";
  145. for (const scriptPromise of scriptsPromises) {
  146. content += await scriptPromise;
  147. }
  148. return content;
  149. }
  150. })();