processor.js 2.5 KB

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