core.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2018 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, singlefile, Blob */
  21. singlefile.core = (() => {
  22. const FORBIDDEN_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  23. const contentScriptFiles = [
  24. "/lib/browser-polyfill/custom-browser-polyfill.js",
  25. "/lib/single-file/doc-helper.js",
  26. "/lib/single-file/base64.js",
  27. "/lib/single-file/parse-srcset.js",
  28. "/lib/fetch/content/fetch.js",
  29. "/lib/single-file/single-file-core.js",
  30. "/lib/single-file/single-file-browser.js",
  31. "/extension/index.js",
  32. "/extension/ui/content/content-ui.js",
  33. "/extension/core/content/content.js"
  34. ];
  35. const frameScriptFiles = [
  36. "/lib/browser-polyfill/custom-browser-polyfill.js",
  37. "/extension/index.js",
  38. "/lib/single-file/doc-helper.js",
  39. "/lib/single-file/timeout.js",
  40. "/lib/single-file/frame-tree.js",
  41. "/extension/core/content/content-frame.js"
  42. ];
  43. const optionalContentScriptFiles = {
  44. compressHTML: [
  45. "/lib/single-file/htmlmini.js",
  46. "/lib/single-file/serializer.js"
  47. ],
  48. compressCSS: [
  49. "/lib/single-file/uglifycss.js"
  50. ],
  51. removeAlternativeFonts: [
  52. "/lib/single-file/fonts-minifier.js"
  53. ],
  54. removeUnusedStyles: [
  55. "/lib/single-file/css-what.js",
  56. "/lib/single-file/parse-css.js",
  57. "/lib/single-file/rules-matcher.js",
  58. "/lib/single-file/css-minifier.js"
  59. ],
  60. lazyLoadImages: [
  61. "/lib/single-file/lazy-loader.js",
  62. ]
  63. };
  64. browser.runtime.onMessage.addListener((request, sender) => {
  65. if (request.getConfig) {
  66. return singlefile.config.get();
  67. }
  68. if (request.processContent) {
  69. processBackgroundTab(sender.tab, request);
  70. }
  71. });
  72. browser.tabs.onRemoved.addListener(async tabId => {
  73. const tabsData = await singlefile.storage.get();
  74. delete tabsData[tabId];
  75. await singlefile.storage.set(tabsData);
  76. });
  77. return { saveTab, autoSaveTab, isAllowedURL };
  78. async function saveTab(tab, processOptions) {
  79. const options = await singlefile.config.get();
  80. Object.keys(processOptions).forEach(key => options[key] = processOptions[key]);
  81. return new Promise(async (resolve, reject) => {
  82. const processPromise = saveStart(tab, options);
  83. try {
  84. await processPromise;
  85. } catch (error) {
  86. reject(error);
  87. }
  88. resolve();
  89. });
  90. }
  91. async function autoSaveTab(tab) {
  92. const options = await singlefile.config.get();
  93. return new Promise(async (resolve, reject) => {
  94. const processPromise = autoSaveStart(tab, options);
  95. try {
  96. await processPromise;
  97. } catch (error) {
  98. reject(error);
  99. }
  100. resolve();
  101. });
  102. }
  103. function isAllowedURL(url) {
  104. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !FORBIDDEN_URLS.find(storeUrl => url.startsWith(storeUrl));
  105. }
  106. async function processBackgroundTab(tab, message) {
  107. const options = await singlefile.config.get();
  108. options.content = message.content;
  109. options.url = message.url;
  110. options.framesData = message.framesData;
  111. options.canvasData = message.canvasData;
  112. options.stylesheetContents = message.stylesheetContents;
  113. options.imageData = message.imageData;
  114. options.insertSingleFileComment = true;
  115. options.insertFaviconLink = true;
  116. options.backgroundTab = true;
  117. options.autoSave = true;
  118. options.incognito = tab.incognito;
  119. let index = 0, maxIndex = 0;
  120. options.onprogress = async event => {
  121. if (event.type == event.RESOURCES_INITIALIZED) {
  122. maxIndex = event.details.max;
  123. }
  124. if (event.type == event.RESOURCES_INITIALIZED || event.type == event.RESOURCE_LOADED) {
  125. index++;
  126. singlefile.ui.button.onProgress(tab.id, index, maxIndex, { autoSave: true });
  127. } else if (event.type == event.PAGE_ENDED) {
  128. singlefile.ui.button.onEnd(tab.id, { autoSave: true });
  129. }
  130. };
  131. const processor = new (SingleFile.getClass())(options);
  132. await processor.initialize();
  133. await processor.preparePageData();
  134. const page = processor.getPageData();
  135. const date = new Date();
  136. page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
  137. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  138. return singlefile.download.downloadPage(page, options);
  139. }
  140. async function saveStart(tab, options) {
  141. if (!options.removeFrames) {
  142. await executeScripts(tab.id, frameScriptFiles, true);
  143. }
  144. await executeScripts(tab.id, getContentScriptFiles(options), false);
  145. if (options.frameId) {
  146. await browser.tabs.sendMessage(tab.id, { saveFrame: true, options }, { frameId: options.frameId });
  147. } else {
  148. await browser.tabs.sendMessage(tab.id, { savePage: true, options });
  149. }
  150. }
  151. async function autoSaveStart(tab, options) {
  152. await browser.tabs.sendMessage(tab.id, { autoSavePage: true, options });
  153. }
  154. async function executeScripts(tabId, scriptFiles, allFrames) {
  155. return Promise.all(scriptFiles.map(file => browser.tabs.executeScript(tabId, { file, allFrames })));
  156. }
  157. function getContentScriptFiles(options) {
  158. let files = contentScriptFiles;
  159. Object.keys(optionalContentScriptFiles).forEach(option => {
  160. if (options[option]) {
  161. files = optionalContentScriptFiles[option].concat(files);
  162. }
  163. });
  164. return files;
  165. }
  166. })();