content-bootstrap.js 4.8 KB

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