core.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/content/content-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. ],
  59. removeAlternativeImages: [
  60. "/lib/single-file/modules/html-images-alt-minifier.js"
  61. ],
  62. removeUnusedFonts: [
  63. "/lib/single-file/vendor/css-font-property-parser.js",
  64. "/lib/single-file/modules/css-fonts-minifier.js"
  65. ],
  66. removeAlternativeFonts: [
  67. "/lib/single-file/modules/css-fonts-alt-minifier.js"
  68. ],
  69. removeUnusedStyles: [
  70. "/lib/single-file/modules/css-matched-rules.js",
  71. "/lib/single-file/modules/css-rules-minifier.js"
  72. ],
  73. removeAlternativeMedias: [
  74. "/lib/single-file/vendor/css-media-query-parser.js",
  75. "/lib/single-file/modules/css-medias-alt-minifier.js"
  76. ]
  77. };
  78. initScripts();
  79. return { saveTab };
  80. async function saveTab(tab, options = {}) {
  81. if (singlefile.util.isAllowedURL(tab.url)) {
  82. await initScripts();
  83. const tabId = tab.id;
  84. options.tabId = tabId;
  85. try {
  86. singlefile.ui.button.onInitialize(tabId, options, 1);
  87. if (options.autoSave) {
  88. const options = await singlefile.config.getOptions(tab.url, true);
  89. if (singlefile.autosave.isEnabled(tab)) {
  90. await singlefile.tabs.sendMessage(tab.id, { autoSavePage: true, options });
  91. }
  92. } else {
  93. const mergedOptions = await singlefile.config.getOptions(tab.url);
  94. Object.keys(options).forEach(key => mergedOptions[key] = options[key]);
  95. if (!mergedOptions.removeFrames) {
  96. await browser.tabs.executeScript(tab.id, { code: frameScript, allFrames: true, runAt: "document_start" });
  97. }
  98. const code = await getContentScript(mergedOptions);
  99. await browser.tabs.executeScript(tab.id, { code, allFrames: false, runAt: "document_idle" });
  100. if (mergedOptions.frameId) {
  101. await singlefile.tabs.sendMessage(tab.id, { saveFrame: true, options: mergedOptions }, { frameId: mergedOptions.frameId });
  102. } else {
  103. await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
  104. }
  105. }
  106. singlefile.ui.button.onInitialize(tabId, options, 2);
  107. } catch (error) {
  108. console.log(error); // eslint-disable-line no-console
  109. singlefile.ui.button.onError(tabId, options);
  110. }
  111. }
  112. }
  113. async function initScripts() {
  114. if (!contentScript && !frameScript && !optionalContentScript) {
  115. optionalContentScript = {};
  116. [contentScript, frameScript] = await Promise.all([
  117. getScript(contentScriptFiles),
  118. getScript(frameScriptFiles),
  119. Promise.all(Object.keys(optionalContentScriptFiles)
  120. .map(async option => optionalContentScript[option] = await getScript(optionalContentScriptFiles[option])))
  121. ]);
  122. }
  123. }
  124. async function getContentScript(options) {
  125. await initScripts();
  126. let script = "";
  127. Object.keys(optionalContentScriptFiles).forEach(option => {
  128. if (options[option]) {
  129. script += optionalContentScript[option] + "\n";
  130. }
  131. });
  132. return script + "\n" + contentScript;
  133. }
  134. async function getScript(scriptFiles) {
  135. const scriptsPromises = scriptFiles.map(async scriptFile => {
  136. if (typeof scriptFile == "function") {
  137. return "(" + scriptFile.toString() + ")();";
  138. } else {
  139. const scriptResource = await fetch(browser.runtime.getURL(scriptFile));
  140. return new TextDecoder().decode(await scriptResource.arrayBuffer());
  141. }
  142. });
  143. let content = "";
  144. for (const scriptPromise of scriptsPromises) {
  145. content += await scriptPromise;
  146. }
  147. return content;
  148. }
  149. })();