content-frame-tree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 window, top, document, addEventListener, MessageChannel, browser, setTimeout */
  24. this.singlefile.lib.frameTree.content.frames = this.singlefile.lib.frameTree.content.frames || (() => {
  25. const singlefile = this.singlefile;
  26. const MESSAGE_PREFIX = "__frameTree__::";
  27. const FRAMES_CSS_SELECTOR = "iframe, frame, object[type=\"text/html\"][data]";
  28. const ALL_ELEMENTS_CSS_SELECTOR = "*";
  29. const INIT_REQUEST_MESSAGE = "initRequest";
  30. const CLEANUP_REQUEST_MESSAGE = "cleanupRequest";
  31. const INIT_RESPONSE_MESSAGE = "frameTree.initResponse";
  32. const TARGET_ORIGIN = "*";
  33. const TIMEOUT_INIT_REQUEST_MESSAGE = 750;
  34. const TOP_WINDOW_ID = "0";
  35. const WINDOW_ID_SEPARATOR = ".";
  36. const TOP_WINDOW = window == top;
  37. const sessions = new Map();
  38. let windowId;
  39. if (TOP_WINDOW) {
  40. windowId = TOP_WINDOW_ID;
  41. if (this.browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
  42. browser.runtime.onMessage.addListener(message => {
  43. if (message.method == INIT_RESPONSE_MESSAGE) {
  44. initResponse(message);
  45. }
  46. });
  47. }
  48. }
  49. addEventListener("message", event => {
  50. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
  51. event.preventDefault();
  52. event.stopPropagation();
  53. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length));
  54. if (!TOP_WINDOW && message.method == INIT_REQUEST_MESSAGE) {
  55. window.stop();
  56. if (message.options.loadDeferredImages && singlefile.lib.lazy.content.loader) {
  57. singlefile.lib.lazy.content.loader.process(message.options);
  58. }
  59. initRequest(message);
  60. } else if (message.method == CLEANUP_REQUEST_MESSAGE) {
  61. cleanupRequest(message);
  62. } else if (!browser.runtime && message.method == INIT_RESPONSE_MESSAGE) {
  63. const port = event.ports[0];
  64. port.onmessage = event => initResponse(event.data);
  65. }
  66. }
  67. }, true);
  68. return {
  69. getAsync: async options => {
  70. const sessionId = options.sessionId || 0;
  71. options = JSON.parse(JSON.stringify(options));
  72. return new Promise(resolve => {
  73. sessions.set(sessionId, { frames: [], resolve });
  74. initRequest({ windowId, sessionId, options });
  75. });
  76. },
  77. getSync: options => {
  78. const sessionId = options.sessionId || 0;
  79. options = JSON.parse(JSON.stringify(options));
  80. sessions.set(sessionId, { frames: [] });
  81. initRequest({ windowId, sessionId, options });
  82. return sessions.get(sessionId).frames;
  83. },
  84. cleanup: options => {
  85. const sessionId = options.sessionId || 0;
  86. cleanupRequest({ windowId, sessionId, options: { sessionId } });
  87. },
  88. initResponse,
  89. TIMEOUT_INIT_REQUEST_MESSAGE
  90. };
  91. function initRequest(message) {
  92. const sessionId = message.sessionId;
  93. if (!TOP_WINDOW) {
  94. windowId = message.windowId;
  95. }
  96. processFrames(document, message.options, windowId, sessionId);
  97. if (!TOP_WINDOW) {
  98. sendInitResponse({ framesData: [getFrameData(document, window, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
  99. delete document.documentElement.dataset.requestedFrameId;
  100. }
  101. }
  102. function cleanupRequest(message) {
  103. const sessionId = message.sessionId;
  104. cleanupFrames(getFrames(document), message.windowId, sessionId);
  105. }
  106. function initResponse(message) {
  107. const windowData = sessions.get(message.sessionId);
  108. if (windowData) {
  109. if (message.requestedFrameId) {
  110. windowData.requestedFrameId = message.requestedFrameId;
  111. }
  112. message.framesData.forEach(messageFrameData => {
  113. let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
  114. if (!frameData) {
  115. frameData = { windowId: messageFrameData.windowId };
  116. windowData.frames.push(frameData);
  117. }
  118. if (!frameData.processed) {
  119. frameData.content = messageFrameData.content;
  120. frameData.baseURI = messageFrameData.baseURI;
  121. frameData.title = messageFrameData.title;
  122. frameData.stylesheetsData = messageFrameData.stylesheetsData;
  123. frameData.imagesData = messageFrameData.imagesData;
  124. frameData.postersData = messageFrameData.postersData;
  125. frameData.canvasData = messageFrameData.canvasData;
  126. frameData.fontsData = messageFrameData.fontsData;
  127. frameData.usedFonts = messageFrameData.usedFonts;
  128. frameData.shadowRootsData = messageFrameData.shadowRootsData;
  129. frameData.processed = messageFrameData.processed;
  130. }
  131. });
  132. const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
  133. if (!remainingFrames) {
  134. windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(WINDOW_ID_SEPARATOR).length - frame1.windowId.split(WINDOW_ID_SEPARATOR).length);
  135. if (windowData.resolve) {
  136. if (windowData.requestedFrameId) {
  137. windowData.frames.forEach(frameData => {
  138. if (frameData.windowId == windowData.requestedFrameId) {
  139. frameData.requestedFrame = true;
  140. }
  141. });
  142. }
  143. windowData.resolve(windowData.frames);
  144. }
  145. }
  146. }
  147. }
  148. function processFrames(doc, options, parentWindowId, sessionId) {
  149. const frameElements = getFrames(doc);
  150. processFramesAsync(doc, frameElements, options, parentWindowId, sessionId);
  151. if (frameElements.length) {
  152. processFramesSync(doc, frameElements, options, parentWindowId, sessionId);
  153. }
  154. }
  155. function processFramesAsync(doc, frameElements, options, parentWindowId, sessionId) {
  156. const framesData = [];
  157. frameElements.forEach((frameElement, frameIndex) => {
  158. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  159. frameElement.setAttribute(singlefile.lib.helper.WIN_ID_ATTRIBUTE_NAME, windowId);
  160. framesData.push({ windowId });
  161. try {
  162. sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options });
  163. } catch (error) {
  164. /* ignored */
  165. }
  166. setTimeout(() => sendInitResponse({ framesData: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
  167. });
  168. sendInitResponse({ framesData, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
  169. delete doc.documentElement.dataset.requestedFrameId;
  170. }
  171. function processFramesSync(doc, frameElements, options, parentWindowId, sessionId) {
  172. const framesData = [];
  173. frameElements.forEach((frameElement, frameIndex) => {
  174. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  175. let frameDoc;
  176. try {
  177. frameDoc = frameElement.contentDocument;
  178. } catch (error) {
  179. // ignored
  180. }
  181. if (frameDoc) {
  182. try {
  183. const frameWindow = frameElement.contentWindow;
  184. frameWindow.stop();
  185. processFrames(frameDoc, options, windowId, sessionId);
  186. framesData.push(getFrameData(frameDoc, frameWindow, windowId, options));
  187. } catch (error) {
  188. framesData.push({ windowId, processed: true });
  189. }
  190. }
  191. });
  192. sendInitResponse({ framesData, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
  193. delete doc.documentElement.dataset.requestedFrameId;
  194. }
  195. function cleanupFrames(frameElements, parentWindowId, sessionId) {
  196. frameElements.forEach((frameElement, frameIndex) => {
  197. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  198. frameElement.removeAttribute(singlefile.lib.helper.WIN_ID_ATTRIBUTE_NAME);
  199. try {
  200. sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId });
  201. } catch (error) {
  202. /* ignored */
  203. }
  204. });
  205. frameElements.forEach((frameElement, frameIndex) => {
  206. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  207. let frameDoc;
  208. try {
  209. frameDoc = frameElement.contentDocument;
  210. } catch (error) {
  211. // ignored
  212. }
  213. if (frameDoc) {
  214. try {
  215. cleanupFrames(getFrames(frameDoc), windowId, sessionId);
  216. } catch (error) {
  217. // ignored
  218. }
  219. }
  220. });
  221. }
  222. function sendInitResponse(message) {
  223. message.method = INIT_RESPONSE_MESSAGE;
  224. try {
  225. top.singlefile.lib.frameTree.content.frames.initResponse(message);
  226. } catch (error) {
  227. sendMessage(top, message, true);
  228. }
  229. }
  230. function sendMessage(targetWindow, message, useChannel) {
  231. if (targetWindow == top && this.browser && browser.runtime && browser.runtime.sendMessage) {
  232. browser.runtime.sendMessage(message);
  233. } else {
  234. if (useChannel) {
  235. const channel = new MessageChannel();
  236. targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method }), TARGET_ORIGIN, [channel.port2]);
  237. channel.port1.postMessage(message);
  238. } else {
  239. targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
  240. }
  241. }
  242. }
  243. function getFrameData(document, window, windowId, options) {
  244. const helper = singlefile.lib.helper;
  245. const docData = helper.preProcessDoc(document, window, options);
  246. const content = helper.serialize(document);
  247. helper.postProcessDoc(document, options);
  248. const baseURI = document.baseURI.split("#")[0];
  249. return {
  250. windowId,
  251. content,
  252. baseURI,
  253. title: document.title,
  254. stylesheetsData: docData.stylesheetsData,
  255. imagesData: docData.imagesData,
  256. postersData: docData.postersData,
  257. canvasData: docData.canvasData,
  258. fontsData: docData.fontsData,
  259. usedFonts: docData.usedFonts,
  260. shadowRootsData: docData.shadowRootsData,
  261. processed: true
  262. };
  263. }
  264. function getFrames(document) {
  265. let frames = Array.from(document.querySelectorAll(FRAMES_CSS_SELECTOR));
  266. document.querySelectorAll(ALL_ELEMENTS_CSS_SELECTOR).forEach(element => {
  267. if (element.shadowRoot) {
  268. frames = frames.concat(...element.shadowRoot.querySelectorAll(FRAMES_CSS_SELECTOR));
  269. }
  270. });
  271. return frames;
  272. }
  273. })();