content-bootstrap.js 4.1 KB

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