content-autosave.js 3.8 KB

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