content-bootstrap.js 3.9 KB

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