content-autosave.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (options.autoSaveDelay && !autoSaveTimeout) {
  37. autoSaveTimeout = setTimeout(() => {
  38. autoSavePage();
  39. }, options.autoSaveDelay * 1000);
  40. } else {
  41. const docData = docHelper.preProcessDoc(document, window, options);
  42. let framesData = [];
  43. if (!options.removeFrames && this.frameTree) {
  44. framesData = await frameTree.getAsync(options);
  45. }
  46. browser.runtime.sendMessage({
  47. saveContent: true,
  48. content: docHelper.serialize(document, false),
  49. canvasData: docData.canvasData,
  50. fontsData: docData.fontsData,
  51. stylesheetContents: docData.stylesheetContents,
  52. imageData: docData.imageData,
  53. postersData: docData.postersData,
  54. framesData,
  55. url: location.href
  56. });
  57. docHelper.postProcessDoc(document, window);
  58. singlefile.pageAutoSaved = true;
  59. }
  60. }
  61. }
  62. async function refresh() {
  63. const [autoSaveEnabled, config] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
  64. options = config;
  65. enableAutoSaveUnload(autoSaveEnabled && (config.autoSaveUnload || config.autoSaveLoadOrUnload));
  66. }
  67. function enableAutoSaveUnload(enabled) {
  68. if (enabled) {
  69. if (!listenerAdded) {
  70. addEventListener("unload", onUnload);
  71. listenerAdded = true;
  72. }
  73. } else {
  74. removeEventListener("unload", onUnload);
  75. listenerAdded = false;
  76. }
  77. }
  78. function onUnload() {
  79. if (!singlefile.pageAutoSaved) {
  80. const docData = docHelper.preProcessDoc(document, window, options);
  81. browser.runtime.sendMessage({
  82. saveContent: true,
  83. content: docHelper.serialize(document),
  84. canvasData: docData.canvasData,
  85. fontsData: docData.fontsData,
  86. stylesheetContents: docData.stylesheetContents,
  87. imageData: docData.imageData,
  88. postersData: docData.postersData,
  89. framesData: this.frameTree && !options.removeFrames && frameTree.getSync(options),
  90. url: location.href
  91. });
  92. }
  93. }
  94. })();