content-bootstrap.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 browser, window, addEventListener, removeEventListener, document, location, setTimeout */
  24. this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.core.content.bootstrap || (async () => {
  25. const singlefile = this.singlefile;
  26. let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage, pageAutoSaved;
  27. browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
  28. options = message.options;
  29. autoSaveEnabled = message.autoSaveEnabled;
  30. refresh();
  31. });
  32. browser.runtime.onMessage.addListener(message => { onMessage(message); });
  33. browser.runtime.sendMessage({ method: "ui.loadURL" });
  34. addEventListener("single-file-push-state", () => browser.runtime.sendMessage({ method: "ui.loadURL" }));
  35. return {};
  36. async function onMessage(message) {
  37. if (autoSaveEnabled && message.method == "content.autosave") {
  38. options = message.options;
  39. await autoSavePage();
  40. if (options.autoSaveRepeat) {
  41. setTimeout(() => {
  42. if (autoSaveEnabled && !autoSavingPage) {
  43. pageAutoSaved = false;
  44. options.autoSaveDelay = 0;
  45. onMessage(message);
  46. }
  47. }, options.autoSaveRepeatDelay * 1000);
  48. }
  49. }
  50. if (message.method == "content.init") {
  51. options = message.options;
  52. autoSaveEnabled = message.autoSaveEnabled;
  53. refresh();
  54. }
  55. }
  56. async function autoSavePage() {
  57. const helper = singlefile.lib.helper;
  58. if ((!autoSavingPage || autoSaveTimeout) && !pageAutoSaved) {
  59. autoSavingPage = true;
  60. if (options.autoSaveDelay && !autoSaveTimeout) {
  61. autoSaveTimeout = setTimeout(() => {
  62. autoSavePage();
  63. }, options.autoSaveDelay * 1000);
  64. } else {
  65. const docData = helper.preProcessDoc(document, window, options);
  66. let frames = [];
  67. autoSaveTimeout = null;
  68. if (!options.removeFrames && singlefile.lib.frameTree.content.frames) {
  69. frames = await singlefile.lib.frameTree.content.frames.getAsync(options);
  70. }
  71. browser.runtime.sendMessage({
  72. method: "autosave.save",
  73. content: helper.serialize(document, false),
  74. canvases: docData.canvases,
  75. fonts: docData.fonts,
  76. stylesheets: docData.stylesheets,
  77. images: docData.images,
  78. posters: docData.posters,
  79. usedFonts: docData.usedFonts,
  80. shadowRoots: docData.shadowRoots,
  81. imports: docData.imports,
  82. referrer: docData.referrer,
  83. frames: frames,
  84. url: location.href
  85. });
  86. helper.postProcessDoc(document, options);
  87. pageAutoSaved = true;
  88. autoSavingPage = false;
  89. }
  90. }
  91. }
  92. async function refresh() {
  93. if (autoSaveEnabled && options && (options.autoSaveUnload || options.autoSaveLoadOrUnload)) {
  94. if (!unloadListenerAdded) {
  95. addEventListener("unload", onUnload);
  96. addEventListener("single-file-push-state", onUnload);
  97. unloadListenerAdded = true;
  98. }
  99. } else {
  100. removeEventListener("unload", onUnload);
  101. removeEventListener("single-file-push-state", onUnload);
  102. unloadListenerAdded = false;
  103. }
  104. }
  105. function onUnload() {
  106. const helper = singlefile.lib.helper;
  107. if (!pageAutoSaved || options.autoSaveUnload) {
  108. const docData = helper.preProcessDoc(document, window, options);
  109. let frames = [];
  110. if (!options.removeFrames && singlefile.lib.frameTree.content.frames) {
  111. frames = singlefile.lib.frameTree.content.frames.getSync(options);
  112. }
  113. browser.runtime.sendMessage({
  114. method: "autosave.save",
  115. content: helper.serialize(document),
  116. canvases: docData.canvases,
  117. fonts: docData.fonts,
  118. stylesheets: docData.stylesheets,
  119. images: docData.images,
  120. posters: docData.posters,
  121. usedFonts: docData.usedFonts,
  122. shadowRoots: docData.shadowRoots,
  123. imports: docData.imports,
  124. referrer: docData.referrer,
  125. frames: frames,
  126. url: location.href
  127. });
  128. }
  129. }
  130. })();