core.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 */
  21. singlefile.core = (() => {
  22. const contentScriptFiles = [
  23. "/lib/hooks/hooks.js",
  24. "/lib/browser-polyfill/chrome-browser-polyfill.js",
  25. "/lib/single-file/vendor/css-tree.js",
  26. "/lib/single-file/vendor/html-srcset-parser.js",
  27. "/lib/single-file/util/timeout.js",
  28. "/lib/single-file/util/doc-helper.js",
  29. "/lib/single-file/util/doc-util-core.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/single-file/util/timeout.js",
  43. "/lib/fetch/content/fetch.js",
  44. "/lib/frame-tree/frame-tree.js",
  45. "/extension/core/content/content-frame.js"
  46. ];
  47. const optionalContentScriptFiles = {
  48. compressHTML: [
  49. "/lib/single-file/modules/html-minifier.js",
  50. "/lib/single-file/modules/html-serializer.js"
  51. ],
  52. compressCSS: [
  53. "/lib/single-file/vendor/css-minifier.js"
  54. ],
  55. loadDeferredImages: [
  56. "/lib/lazy/content/content-lazy-loader.js"
  57. ],
  58. removeAlternativeImages: [
  59. "/lib/single-file/modules/html-images-alt-minifier.js"
  60. ],
  61. removeUnusedFonts: [
  62. "/lib/single-file/vendor/css-font-property-parser.js",
  63. "/lib/single-file/modules/css-fonts-minifier.js"
  64. ],
  65. removeAlternativeFonts: [
  66. "/lib/single-file/modules/css-fonts-alt-minifier.js"
  67. ],
  68. removeUnusedStyles: [
  69. "/lib/single-file/modules/css-matched-rules.js",
  70. "/lib/single-file/modules/css-rules-minifier.js"
  71. ],
  72. removeAlternativeMedias: [
  73. "/lib/single-file/vendor/css-media-query-parser.js",
  74. "/lib/single-file/modules/css-medias-alt-minifier.js"
  75. ]
  76. };
  77. return { saveTab };
  78. async function saveTab(tab, options = {}) {
  79. if (singlefile.util.isAllowedURL(tab.url)) {
  80. const tabId = tab.id;
  81. options.tabId = tabId;
  82. try {
  83. singlefile.ui.button.onInitialize(tabId, options, 1);
  84. if (options.autoSave) {
  85. const options = await singlefile.config.getOptions(tab.url, true);
  86. if (singlefile.autosave.isEnabled(tab)) {
  87. await singlefile.tabs.sendMessage(tab.id, { autoSavePage: true, options });
  88. }
  89. } else {
  90. const mergedOptions = await singlefile.config.getOptions(tab.url);
  91. Object.keys(options).forEach(key => mergedOptions[key] = options[key]);
  92. if (!mergedOptions.removeFrames) {
  93. await executeContentScripts(tab.id, frameScriptFiles, true, "document_start");
  94. }
  95. await executeContentScripts(tab.id, getContentScriptFiles(mergedOptions), false, "document_idle");
  96. if (mergedOptions.frameId) {
  97. await singlefile.tabs.sendMessage(tab.id, { saveFrame: true, options: mergedOptions }, { frameId: mergedOptions.frameId });
  98. } else {
  99. await singlefile.tabs.sendMessage(tab.id, { savePage: true, options: mergedOptions });
  100. }
  101. }
  102. singlefile.ui.button.onInitialize(tabId, options, 2);
  103. } catch (error) {
  104. console.log(error); // eslint-disable-line no-console
  105. singlefile.ui.button.onError(tabId, options);
  106. }
  107. }
  108. }
  109. async function executeContentScripts(tabId, scriptFiles, allFrames, runAt) {
  110. for (const file of scriptFiles) {
  111. await browser.tabs.executeScript(tabId, { file, allFrames, runAt });
  112. }
  113. }
  114. function getContentScriptFiles(options) {
  115. let files = contentScriptFiles;
  116. Object.keys(optionalContentScriptFiles).forEach(option => {
  117. if (options[option]) {
  118. files = optionalContentScriptFiles[option].concat(files);
  119. }
  120. });
  121. return files;
  122. }
  123. })();