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