content-autosave.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 singlefile, frameTree, browser, window, addEventListener, removeEventListener, document, location, docHelper */
  21. this.singlefile.autosave = this.singlefile.autosave || (async () => {
  22. let listenerAdded, options, autoSaveTimeout;
  23. refresh();
  24. browser.runtime.onMessage.addListener(message => {
  25. if (message.autoSavePage) {
  26. autoSavePage();
  27. }
  28. if (message.autoSaveUnloadEnabled) {
  29. refresh();
  30. }
  31. });
  32. return {};
  33. async function autoSavePage() {
  34. const [autoSaveEnabled, options] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
  35. if (autoSaveEnabled) {
  36. options.sessionId = 0;
  37. if (options.autoSaveDelay && !autoSaveTimeout) {
  38. autoSaveTimeout = setTimeout(() => {
  39. autoSavePage();
  40. }, options.autoSaveDelay * 1000);
  41. } else {
  42. const docData = docHelper.preProcessDoc(document, window, options);
  43. let framesData = [];
  44. if (!options.removeFrames && this.frameTree) {
  45. framesData = await frameTree.getAsync(options);
  46. }
  47. browser.runtime.sendMessage({
  48. saveContent: true,
  49. content: docHelper.serialize(document, false),
  50. canvasData: docData.canvasData,
  51. fontsData: docData.fontsData,
  52. stylesheetContents: docData.stylesheetContents,
  53. responsiveImageData: docData.responsiveImageData,
  54. imageData: docData.imageData,
  55. postersData: docData.postersData,
  56. usedFonts: docData.usedFonts,
  57. framesData,
  58. url: location.href
  59. });
  60. docHelper.postProcessDoc(document, options);
  61. singlefile.pageAutoSaved = true;
  62. }
  63. }
  64. }
  65. async function refresh() {
  66. const [autoSaveEnabled, config] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
  67. options = config;
  68. enableAutoSaveUnload(autoSaveEnabled && (config.autoSaveUnload || config.autoSaveLoadOrUnload));
  69. }
  70. function enableAutoSaveUnload(enabled) {
  71. if (enabled) {
  72. if (!listenerAdded) {
  73. addEventListener("unload", onUnload);
  74. listenerAdded = true;
  75. }
  76. } else {
  77. removeEventListener("unload", onUnload);
  78. listenerAdded = false;
  79. }
  80. }
  81. function onUnload() {
  82. if (!singlefile.pageAutoSaved) {
  83. const docData = docHelper.preProcessDoc(document, window, options);
  84. const framesData = (typeof frameTree != "undefined") && !options.removeFrames && frameTree.getSync(options);
  85. browser.runtime.sendMessage({
  86. saveContent: true,
  87. content: docHelper.serialize(document),
  88. canvasData: docData.canvasData,
  89. fontsData: docData.fontsData,
  90. stylesheetContents: docData.stylesheetContents,
  91. responsiveImageData: docData.responsiveImageData,
  92. imageData: docData.imageData,
  93. postersData: docData.postersData,
  94. usedFonts: docData.usedFonts,
  95. framesData,
  96. url: location.href
  97. });
  98. }
  99. }
  100. })();