content-bootstrap.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global singlefile, frameTree, browser, window, addEventListener, removeEventListener, document, location, docHelper, setTimeout */
  24. this.singlefile.bootstrap = this.singlefile.bootstrap || (async () => {
  25. let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage;
  26. browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
  27. options = message.options;
  28. autoSaveEnabled = message.autoSaveEnabled;
  29. refresh();
  30. });
  31. browser.runtime.onMessage.addListener(message => onMessage(message));
  32. browser.runtime.sendMessage({ method: "ui.loadURL" });
  33. return {};
  34. async function onMessage(message) {
  35. if (message.method == "content.autosave") {
  36. autoSavingPage = false;
  37. singlefile.pageAutoSaved = false;
  38. options = message.options;
  39. await autoSavePage();
  40. if (options.autoSaveRepeat) {
  41. setTimeout(() => onMessage(message), options.autoSaveRepeatDelay * 1000);
  42. }
  43. }
  44. if (message.method == "content.init") {
  45. options = message.options;
  46. autoSaveEnabled = message.autoSaveEnabled;
  47. refresh();
  48. }
  49. }
  50. async function autoSavePage() {
  51. if ((!autoSavingPage || autoSaveTimeout) && !singlefile.pageAutoSaved) {
  52. autoSavingPage = true;
  53. if (options.autoSaveDelay && !autoSaveTimeout) {
  54. autoSaveTimeout = setTimeout(() => {
  55. autoSavePage();
  56. }, options.autoSaveDelay * 1000);
  57. } else {
  58. const docData = docHelper.preProcessDoc(document, window, options);
  59. let framesData = [];
  60. autoSaveTimeout = null;
  61. if (!options.removeFrames && this.frameTree) {
  62. framesData = await frameTree.getAsync(options);
  63. }
  64. browser.runtime.sendMessage({
  65. method: "autosave.save",
  66. content: docHelper.serialize(document, false),
  67. canvasData: docData.canvasData,
  68. fontsData: docData.fontsData,
  69. stylesheetsData: docData.stylesheetsData,
  70. imageData: docData.imageData,
  71. postersData: docData.postersData,
  72. usedFonts: docData.usedFonts,
  73. shadowRootsData: docData.shadowRootsData,
  74. referrer: docData.referrer,
  75. framesData,
  76. url: location.href
  77. });
  78. docHelper.postProcessDoc(document, options);
  79. singlefile.pageAutoSaved = true;
  80. autoSavingPage = false;
  81. }
  82. }
  83. }
  84. async function refresh() {
  85. if (autoSaveEnabled && options && (options.autoSaveUnload || options.autoSaveLoadOrUnload)) {
  86. if (!unloadListenerAdded) {
  87. addEventListener("unload", onUnload);
  88. addEventListener("single-file-push-state", onUnload);
  89. unloadListenerAdded = true;
  90. }
  91. } else {
  92. removeEventListener("unload", onUnload);
  93. removeEventListener("single-file-push-state", onUnload);
  94. unloadListenerAdded = false;
  95. }
  96. }
  97. function onUnload() {
  98. if (!singlefile.pageAutoSaved || options.autoSaveUnload) {
  99. const docData = docHelper.preProcessDoc(document, window, options);
  100. let framesData = [];
  101. if (this.frameTree && !options.removeFrames) {
  102. framesData = frameTree.getSync(options);
  103. }
  104. browser.runtime.sendMessage({
  105. method: "autosave.save",
  106. content: docHelper.serialize(document),
  107. canvasData: docData.canvasData,
  108. fontsData: docData.fontsData,
  109. stylesheetsData: docData.stylesheetsData,
  110. imageData: docData.imageData,
  111. postersData: docData.postersData,
  112. usedFonts: docData.usedFonts,
  113. shadowRootsData: docData.shadowRootsData,
  114. referrer: docData.referrer,
  115. framesData,
  116. url: location.href
  117. });
  118. }
  119. }
  120. })();