core.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "/extension/index.js",
  26. "/lib/single-file/doc-helper.js",
  27. "/extension/ui/content/content-ui.js",
  28. "/lib/single-file/base64.js",
  29. "/lib/single-file/uglifycss.js",
  30. "/lib/single-file/css-what.js",
  31. "/lib/single-file/parse-css.js",
  32. "/lib/single-file/fonts-minifier.js",
  33. "/lib/single-file/css-minifier.js",
  34. "/lib/single-file/htmlmini.js",
  35. "/lib/single-file/parse-srcset.js",
  36. "/lib/single-file/lazy-loader.js",
  37. "/lib/single-file/serializer.js",
  38. "/lib/single-file/single-file-core.js",
  39. "/lib/single-file/single-file-browser.js",
  40. "/lib/fetch/content/fetch.js",
  41. "/extension/core/content/content.js"
  42. ];
  43. browser.runtime.onMessage.addListener((request, sender) => {
  44. if (request.getConfig) {
  45. return singlefile.config.get();
  46. }
  47. if (request.download) {
  48. try {
  49. if (request.content) {
  50. request.url = URL.createObjectURL(new Blob([request.content], { type: "text/html" }));
  51. }
  52. return downloadPage(request, { confirmFilename: request.confirmFilename })
  53. .catch(() => ({ notSupported: true }));
  54. } catch (error) {
  55. return Promise.resolve({ notSupported: true });
  56. }
  57. }
  58. if (request.processContent) {
  59. processBackgroundTab(sender.tab.id, request);
  60. }
  61. });
  62. browser.tabs.onRemoved.addListener(async tabId => {
  63. const tabsData = await singlefile.storage.get();
  64. delete tabsData[tabId];
  65. await singlefile.storage.set(tabsData);
  66. });
  67. return {
  68. async processTab(tab, processOptions = {}) {
  69. const options = await singlefile.config.get();
  70. Object.keys(processOptions).forEach(key => options[key] = processOptions[key]);
  71. return new Promise(async (resolve, reject) => {
  72. const processPromise = processStart(tab, options);
  73. try {
  74. await processPromise;
  75. } catch (error) {
  76. reject(error);
  77. }
  78. resolve();
  79. });
  80. },
  81. isAllowedURL(url) {
  82. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !FORBIDDEN_URLS.find(storeUrl => url.startsWith(storeUrl));
  83. }
  84. };
  85. async function processBackgroundTab(tabId, message) {
  86. const options = await singlefile.config.get();
  87. options.content = message.content;
  88. options.url = message.url;
  89. options.framesData = message.framesData;
  90. options.canvasData = message.canvasData;
  91. options.emptyStyleRulesText = message.emptyStyleRulesText;
  92. options.insertSingleFileComment = true;
  93. options.insertFaviconLink = true;
  94. options.backgroundTab = true;
  95. options.autoSave = true;
  96. options.onprogress = async event => {
  97. if (event.type == event.RESOURCES_INITIALIZED || event.type == event.RESOURCE_LOADED) {
  98. singlefile.ui.button.onProgress(tabId, event.details.index, event.details.max, { autoSave: true });
  99. } else if (event.type == event.PAGE_ENDED) {
  100. singlefile.ui.button.onEnd(tabId, { autoSave: true });
  101. }
  102. };
  103. const processor = new (SingleFile.getClass())(options);
  104. await processor.initialize();
  105. await processor.preparePageData();
  106. const page = processor.getPageData();
  107. const date = new Date();
  108. page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
  109. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  110. return downloadPage(page, options);
  111. }
  112. async function downloadPage(page, options) {
  113. const downloadId = await browser.downloads.download({ url: page.url, saveAs: options.confirmFilename, filename: page.filename.replace(/[/\\?%*:|"<>]+/g, "_") });
  114. return new Promise(resolve => {
  115. URL.revokeObjectURL(page.url);
  116. browser.downloads.onChanged.addListener(onChanged);
  117. function onChanged(event) {
  118. if (event.id == downloadId && event.state && event.state.current == "complete") {
  119. resolve({});
  120. browser.downloads.onChanged.removeListener(onChanged);
  121. }
  122. }
  123. });
  124. }
  125. async function processStart(tab, options) {
  126. await executeScripts(tab.id, contentScriptFiles, { allFrames: false });
  127. if (options.frameId) {
  128. await browser.tabs.sendMessage(tab.id, { processStartFrame: true, options }, { frameId: options.frameId });
  129. } else {
  130. await browser.tabs.sendMessage(tab.id, { processStart: true, options });
  131. }
  132. }
  133. async function executeScripts(tabId, scriptFiles, details) {
  134. for (let file of scriptFiles) {
  135. details.file = file;
  136. await browser.tabs.executeScript(tabId, details);
  137. }
  138. }
  139. })();