content-autosave.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, history, HTMLDocument, dispatchEvent, CustomEvent, 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. if (document instanceof HTMLDocument) {
  35. const scriptElement = document.createElement("script");
  36. scriptElement.textContent = `(${hookPushState.toString()})()`;
  37. document.documentElement.appendChild(scriptElement);
  38. scriptElement.remove();
  39. }
  40. return {};
  41. async function autoSavePage() {
  42. if ((!autoSavingPage || autoSaveTimeout) && !singlefile.pageAutoSaved) {
  43. autoSavingPage = true;
  44. const [autoSaveEnabled, options] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
  45. if (autoSaveEnabled) {
  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. } else {
  76. autoSavingPage = false;
  77. }
  78. }
  79. }
  80. async function refresh() {
  81. const [autoSaveEnabled, config] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
  82. options = config;
  83. enableAutoSaveUnload(autoSaveEnabled && (config.autoSaveUnload || config.autoSaveLoadOrUnload));
  84. }
  85. function enableAutoSaveUnload(enabled) {
  86. if (enabled) {
  87. if (!listenerAdded) {
  88. addEventListener("unload", onUnload);
  89. addEventListener("single-file-push-state", onUnload);
  90. listenerAdded = true;
  91. }
  92. } else {
  93. removeEventListener("unload", onUnload);
  94. removeEventListener("single-file-push-state", onUnload);
  95. listenerAdded = false;
  96. }
  97. }
  98. function onUnload() {
  99. if (!singlefile.pageAutoSaved || options.autoSaveUnload) {
  100. options.sessionId = 0;
  101. const docData = docHelper.preProcessDoc(document, window, options);
  102. const framesData = (typeof frameTree != "undefined") && !options.removeFrames && frameTree.getSync(options);
  103. browser.runtime.sendMessage({
  104. saveContent: true,
  105. content: docHelper.serialize(document),
  106. canvasData: docData.canvasData,
  107. fontsData: docData.fontsData,
  108. stylesheetContents: docData.stylesheetContents,
  109. imageData: docData.imageData,
  110. postersData: docData.postersData,
  111. usedFonts: docData.usedFonts,
  112. shadowRootContents: docData.shadowRootContents,
  113. framesData,
  114. url: location.href
  115. });
  116. }
  117. }
  118. function hookPushState() {
  119. console.warn("SingleFile is hooking the history.pushState API to detect navigation."); // eslint-disable-line no-console
  120. const pushState = history.pushState;
  121. history.pushState = function (state, title, url) {
  122. dispatchEvent(new CustomEvent("single-file-push-state", { detail: { state, title, url } }));
  123. pushState.call(history, state, title, url);
  124. };
  125. }
  126. })();