processor.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, SingleFileBrowser, singlefile, Blob, URL */
  21. singlefile.processor = (() => {
  22. browser.runtime.onMessage.addListener((request, sender) => {
  23. if (request.saveContent) {
  24. saveContent(request, sender.tab.id, sender.tab.incognito);
  25. }
  26. });
  27. return true;
  28. async function saveContent(message, tabId, incognito) {
  29. const options = await singlefile.config.get();
  30. options.content = message.content;
  31. options.url = message.url;
  32. options.framesData = message.framesData;
  33. options.canvasData = message.canvasData;
  34. options.fontsData = message.fontsData;
  35. options.stylesheetContents = message.stylesheetContents;
  36. options.imageData = message.imageData;
  37. options.postersData = message.postersData;
  38. options.usedFonts = message.usedFonts;
  39. options.shadowRootContents = message.shadowRootContents;
  40. options.insertSingleFileComment = true;
  41. options.insertFaviconLink = true;
  42. options.backgroundTab = true;
  43. options.autoSave = true;
  44. options.incognito = incognito;
  45. options.tabId = tabId;
  46. options.sessionId = 0;
  47. let index = 0, maxIndex = 0;
  48. options.onprogress = async event => {
  49. if (event.type == event.RESOURCES_INITIALIZED) {
  50. maxIndex = event.detail.max;
  51. singlefile.ui.button.onProgress(tabId, index, maxIndex, { autoSave: true });
  52. }
  53. if (event.type == event.RESOURCE_LOADED) {
  54. index++;
  55. singlefile.ui.button.onProgress(tabId, index, maxIndex, { autoSave: true });
  56. } else if (event.type == event.PAGE_ENDED) {
  57. singlefile.ui.button.onEnd(tabId, { autoSave: true });
  58. }
  59. };
  60. const processor = new (SingleFileBrowser.getClass())(options);
  61. await processor.initialize();
  62. await processor.run();
  63. const page = await processor.getPageData();
  64. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  65. return singlefile.download.downloadPage(page, options);
  66. }
  67. })();