content-bootstrap.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, CustomEvent, dispatchEvent */
  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. singlefile.extension.core.content.updatedResources = {};
  28. browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
  29. options = message.options;
  30. autoSaveEnabled = message.autoSaveEnabled;
  31. refresh();
  32. });
  33. browser.runtime.onMessage.addListener(message => { onMessage(message); });
  34. browser.runtime.sendMessage({ method: "ui.processInit" });
  35. addEventListener("single-file-push-state", () => browser.runtime.sendMessage({ method: "ui.processInit" }));
  36. addEventListener("single-file-user-script-init", () => singlefile.waitForUserScript = async eventPrefixName => {
  37. const event = new CustomEvent(eventPrefixName + "-request", { cancelable: true });
  38. const promiseResponse = new Promise(resolve => addEventListener(eventPrefixName + "-response", resolve));
  39. dispatchEvent(event);
  40. if (event.defaultPrevented) {
  41. await promiseResponse;
  42. }
  43. });
  44. return {};
  45. async function onMessage(message) {
  46. if (autoSaveEnabled && message.method == "content.autosave") {
  47. options = message.options;
  48. await autoSavePage();
  49. if (options.autoSaveRepeat) {
  50. setTimeout(() => {
  51. if (autoSaveEnabled && !autoSavingPage) {
  52. pageAutoSaved = false;
  53. options.autoSaveDelay = 0;
  54. onMessage(message);
  55. }
  56. }, options.autoSaveRepeatDelay * 1000);
  57. }
  58. }
  59. if (message.method == "content.init") {
  60. options = message.options;
  61. autoSaveEnabled = message.autoSaveEnabled;
  62. refresh();
  63. }
  64. if (message.method == "devtools.resourceCommitted") {
  65. singlefile.extension.core.content.updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  66. }
  67. }
  68. async function autoSavePage() {
  69. const helper = singlefile.lib.helper;
  70. if ((!autoSavingPage || autoSaveTimeout) && !pageAutoSaved) {
  71. autoSavingPage = true;
  72. if (options.autoSaveDelay && !autoSaveTimeout) {
  73. await new Promise(resolve => autoSaveTimeout = setTimeout(resolve, options.autoSaveDelay * 1000));
  74. await autoSavePage();
  75. } else {
  76. let frames = [];
  77. autoSaveTimeout = null;
  78. if (!options.removeFrames && singlefile.lib.frameTree.content.frames && window.frames && window.frames.length) {
  79. frames = await singlefile.lib.frameTree.content.frames.getAsync(options);
  80. }
  81. if (options.userScriptEnabled && singlefile.waitForUserScript) {
  82. await singlefile.waitForUserScript("single-file-on-before-capture");
  83. }
  84. const docData = helper.preProcessDoc(document, window, options);
  85. savePage(docData, frames);
  86. helper.postProcessDoc(document, docData.markedElements);
  87. if (options.userScriptEnabled && singlefile.waitForUserScript) {
  88. await singlefile.waitForUserScript("single-file-on-after-capture");
  89. }
  90. pageAutoSaved = true;
  91. autoSavingPage = false;
  92. }
  93. }
  94. }
  95. async function refresh() {
  96. if (autoSaveEnabled && options && (options.autoSaveUnload || options.autoSaveLoadOrUnload)) {
  97. if (!unloadListenerAdded) {
  98. addEventListener("unload", onUnload);
  99. addEventListener("single-file-push-state", onUnload);
  100. unloadListenerAdded = true;
  101. }
  102. } else {
  103. removeEventListener("unload", onUnload);
  104. removeEventListener("single-file-push-state", onUnload);
  105. unloadListenerAdded = false;
  106. }
  107. }
  108. function onUnload() {
  109. const helper = singlefile.lib.helper;
  110. if (!pageAutoSaved || options.autoSaveUnload) {
  111. let frames = [];
  112. if (!options.removeFrames && singlefile.lib.frameTree.content.frames && window.frames && window.frames.length) {
  113. frames = singlefile.lib.frameTree.content.frames.getSync(options);
  114. }
  115. if (options.userScriptEnabled && singlefile.waitForUserScript) {
  116. singlefile.waitForUserScript("single-file-on-before-capture");
  117. }
  118. const docData = helper.preProcessDoc(document, window, options);
  119. savePage(docData, frames);
  120. }
  121. }
  122. function savePage(docData, frames) {
  123. const helper = singlefile.lib.helper;
  124. const updatedResources = singlefile.extension.core.content.updatedResources;
  125. Object.keys(updatedResources).forEach(url => updatedResources[url].retrieved = false);
  126. browser.runtime.sendMessage({
  127. method: "autosave.save",
  128. content: helper.serialize(document),
  129. canvases: docData.canvases,
  130. fonts: docData.fonts,
  131. stylesheets: docData.stylesheets,
  132. images: docData.images,
  133. posters: docData.posters,
  134. usedFonts: docData.usedFonts,
  135. shadowRoots: docData.shadowRoots,
  136. imports: docData.imports,
  137. referrer: docData.referrer,
  138. frames: frames,
  139. url: location.href,
  140. updatedResources
  141. });
  142. }
  143. })();