processor.js 2.4 KB

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