content-bootstrap.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, prompt, Node */
  24. this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.core.content.bootstrap || (async () => {
  25. const singlefile = this.singlefile;
  26. const MAX_CONTENT_SIZE = 32 * (1024 * 1024);
  27. const PUSH_STATE_NOTIFICATION_EVENT_NAME = "single-file-push-state";
  28. let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage, pageAutoSaved;
  29. singlefile.extension.core.content.updatedResources = {};
  30. browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
  31. options = message.options;
  32. autoSaveEnabled = message.autoSaveEnabled;
  33. if (document.documentElement.firstChild.nodeType == Node.COMMENT_NODE && document.documentElement.firstChild.textContent.includes("Page saved with SingleFile") && options.autoOpenEditor) {
  34. if (document.readyState == "loading") {
  35. document.addEventListener("DOMContentLoaded", () => openEditor(document));
  36. } else {
  37. openEditor(document);
  38. }
  39. } else {
  40. refresh();
  41. }
  42. });
  43. browser.runtime.onMessage.addListener(message => { onMessage(message); });
  44. browser.runtime.sendMessage({ method: "ui.processInit" });
  45. addEventListener(PUSH_STATE_NOTIFICATION_EVENT_NAME, () => browser.runtime.sendMessage({ method: "ui.processInit" }));
  46. return {};
  47. async function onMessage(message) {
  48. if (autoSaveEnabled && message.method == "content.autosave") {
  49. options = message.options;
  50. await autoSavePage();
  51. if (options.autoSaveRepeat) {
  52. setTimeout(() => {
  53. if (autoSaveEnabled && !autoSavingPage) {
  54. pageAutoSaved = false;
  55. options.autoSaveDelay = 0;
  56. onMessage(message);
  57. }
  58. }, options.autoSaveRepeatDelay * 1000);
  59. }
  60. }
  61. if (message.method == "content.init") {
  62. options = message.options;
  63. autoSaveEnabled = message.autoSaveEnabled;
  64. refresh();
  65. return {};
  66. }
  67. if (message.method == "devtools.resourceCommitted") {
  68. singlefile.extension.core.content.updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  69. }
  70. if (message.method == "common.promptValueRequest") {
  71. browser.runtime.sendMessage({ method: "tabs.promptValueResponse", value: prompt("SingleFile: " + message.promptMessage) });
  72. }
  73. }
  74. async function autoSavePage() {
  75. const helper = singlefile.lib.helper;
  76. if ((!autoSavingPage || autoSaveTimeout) && !pageAutoSaved) {
  77. autoSavingPage = true;
  78. if (options.autoSaveDelay && !autoSaveTimeout) {
  79. await new Promise(resolve => autoSaveTimeout = setTimeout(resolve, options.autoSaveDelay * 1000));
  80. await autoSavePage();
  81. } else {
  82. let frames = [];
  83. autoSaveTimeout = null;
  84. if (!options.removeFrames && singlefile.lib.processors.frameTree.content.frames && window.frames && window.frames.length) {
  85. frames = await singlefile.lib.processors.frameTree.content.frames.getAsync(options);
  86. }
  87. if (options.userScriptEnabled && helper.waitForUserScript) {
  88. await helper.waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
  89. }
  90. const docData = helper.preProcessDoc(document, window, options);
  91. savePage(docData, frames);
  92. helper.postProcessDoc(document, docData.markedElements);
  93. if (options.userScriptEnabled && helper.waitForUserScript) {
  94. await helper.waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
  95. }
  96. pageAutoSaved = true;
  97. autoSavingPage = false;
  98. }
  99. }
  100. }
  101. async function refresh() {
  102. if (autoSaveEnabled && options && (options.autoSaveUnload || options.autoSaveLoadOrUnload)) {
  103. if (!unloadListenerAdded) {
  104. addEventListener("unload", onUnload);
  105. addEventListener(PUSH_STATE_NOTIFICATION_EVENT_NAME, onUnload);
  106. unloadListenerAdded = true;
  107. }
  108. } else {
  109. removeEventListener("unload", onUnload);
  110. removeEventListener(PUSH_STATE_NOTIFICATION_EVENT_NAME, onUnload);
  111. unloadListenerAdded = false;
  112. }
  113. }
  114. function onUnload() {
  115. const helper = singlefile.lib.helper;
  116. if (!pageAutoSaved || options.autoSaveUnload) {
  117. let frames = [];
  118. if (!options.removeFrames && singlefile.lib.processors.frameTree.content.frames && window.frames && window.frames.length) {
  119. frames = singlefile.lib.processors.frameTree.content.frames.getSync(options);
  120. }
  121. if (options.userScriptEnabled && helper.waitForUserScript) {
  122. helper.waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
  123. }
  124. const docData = helper.preProcessDoc(document, window, options);
  125. savePage(docData, frames);
  126. }
  127. }
  128. function savePage(docData, frames) {
  129. const helper = singlefile.lib.helper;
  130. const updatedResources = singlefile.extension.core.content.updatedResources;
  131. Object.keys(updatedResources).forEach(url => updatedResources[url].retrieved = false);
  132. browser.runtime.sendMessage({
  133. method: "autosave.save",
  134. taskId: options.taskId,
  135. content: helper.serialize(document),
  136. canvases: docData.canvases,
  137. fonts: docData.fonts,
  138. stylesheets: docData.stylesheets,
  139. images: docData.images,
  140. posters: docData.posters,
  141. usedFonts: docData.usedFonts,
  142. shadowRoots: docData.shadowRoots,
  143. imports: docData.imports,
  144. referrer: docData.referrer,
  145. frames: frames,
  146. url: location.href,
  147. updatedResources
  148. });
  149. }
  150. async function openEditor(document) {
  151. serializeShadowRoots(document);
  152. const content = singlefile.lib.modules.serializer.process(document);
  153. for (let blockIndex = 0; blockIndex * MAX_CONTENT_SIZE < content.length; blockIndex++) {
  154. const message = {
  155. method: "editor.open",
  156. filename: decodeURIComponent(location.href.match(/^.*\/(.*)$/)[1])
  157. };
  158. message.truncated = content.length > MAX_CONTENT_SIZE;
  159. if (message.truncated) {
  160. message.finished = (blockIndex + 1) * MAX_CONTENT_SIZE > content.length;
  161. message.content = content.substring(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE);
  162. } else {
  163. message.content = content;
  164. }
  165. await browser.runtime.sendMessage(message);
  166. }
  167. }
  168. function serializeShadowRoots(node) {
  169. const SHADOW_MODE_ATTRIBUTE_NAME = "shadowmode";
  170. node.querySelectorAll("*").forEach(element => {
  171. if (element.shadowRoot) {
  172. serializeShadowRoots(element.shadowRoot);
  173. const templateElement = document.createElement("template");
  174. templateElement.setAttribute(SHADOW_MODE_ATTRIBUTE_NAME, "open");
  175. templateElement.appendChild(element.shadowRoot);
  176. element.appendChild(templateElement);
  177. }
  178. });
  179. }
  180. })();