processor.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.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.insertSingleFileComment = true;
  39. options.insertFaviconLink = true;
  40. options.backgroundTab = true;
  41. options.autoSave = true;
  42. options.incognito = incognito;
  43. options.tabId = tabId;
  44. let index = 0, maxIndex = 0;
  45. options.onprogress = async event => {
  46. if (event.type == event.RESOURCES_INITIALIZED) {
  47. maxIndex = event.details.max;
  48. singlefile.ui.button.onProgress(tabId, index, maxIndex, { autoSave: true });
  49. }
  50. if (event.type == event.RESOURCE_LOADED) {
  51. index++;
  52. singlefile.ui.button.onProgress(tabId, index, maxIndex, { autoSave: true });
  53. } else if (event.type == event.PAGE_ENDED) {
  54. singlefile.ui.button.onEnd(tabId, { autoSave: true });
  55. }
  56. };
  57. const processor = new (SingleFile.getClass())(options);
  58. await processor.initialize();
  59. await processor.preparePageData();
  60. const page = await processor.getPageData();
  61. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  62. return singlefile.download.downloadPage(page, options);
  63. }
  64. })();