bg.js 4.8 KB

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