core.js 5.2 KB

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