core.js 5.2 KB

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