core.js 4.9 KB

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