core.js 4.9 KB

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