bg.js 4.3 KB

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