content-frame-tree.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global window, top, document, addEventListener, docHelper, MessageChannel, lazyLoader, browser, setTimeout */
  21. this.frameTree = this.frameTree || (() => {
  22. const MESSAGE_PREFIX = "__frameTree__::";
  23. const FRAMES_CSS_SELECTOR = "iframe, frame, object[type=\"text/html\"][data]";
  24. const INIT_REQUEST_MESSAGE = "initRequest";
  25. const CLEANUP_REQUEST_MESSAGE = "cleanupRequest";
  26. const INIT_RESPONSE_MESSAGE = "initResponse";
  27. const TARGET_ORIGIN = "*";
  28. const TIMEOUT_INIT_REQUEST_MESSAGE = 750;
  29. const TOP_WINDOW_ID = "0";
  30. const WINDOW_ID_SEPARATOR = ".";
  31. const TOP_WINDOW = window == top;
  32. const sessions = new Map();
  33. let windowId;
  34. if (TOP_WINDOW) {
  35. windowId = TOP_WINDOW_ID;
  36. if (this.browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
  37. browser.runtime.onMessage.addListener(message => {
  38. if (message.method == INIT_RESPONSE_MESSAGE) {
  39. initResponse(message);
  40. }
  41. });
  42. }
  43. }
  44. addEventListener("message", event => {
  45. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
  46. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length));
  47. if (!TOP_WINDOW && message.method == INIT_REQUEST_MESSAGE) {
  48. window.stop();
  49. initRequest(message);
  50. if (message.options.loadDeferredImages && window.lazyLoader) {
  51. lazyLoader.process(message.options);
  52. }
  53. } else if (message.method == CLEANUP_REQUEST_MESSAGE) {
  54. cleanupRequest(message);
  55. } else if (message.method == INIT_RESPONSE_MESSAGE) {
  56. const port = event.ports[0];
  57. port.onmessage = event => initResponse(event.data);
  58. }
  59. event.preventDefault();
  60. event.stopPropagation();
  61. }
  62. }, true);
  63. return {
  64. getAsync: async options => {
  65. const sessionId = options.sessionId || 0;
  66. options = JSON.parse(JSON.stringify(options));
  67. return new Promise(resolve => {
  68. sessions.set(sessionId, { frames: [], resolve });
  69. initRequest({ windowId, sessionId, options });
  70. });
  71. },
  72. getSync: options => {
  73. const sessionId = options.sessionId || 0;
  74. options = JSON.parse(JSON.stringify(options));
  75. sessions.set(sessionId, { frames: [] });
  76. initRequest({ windowId, sessionId, options });
  77. return sessions.get(sessionId).frames;
  78. },
  79. cleanup: options => {
  80. const sessionId = options.sessionId || 0;
  81. options = JSON.parse(JSON.stringify(options));
  82. cleanupRequest({ windowId, sessionId, options });
  83. },
  84. initResponse,
  85. TIMEOUT_INIT_REQUEST_MESSAGE
  86. };
  87. function initRequest(message) {
  88. const sessionId = message.sessionId;
  89. if (!TOP_WINDOW) {
  90. windowId = message.windowId;
  91. }
  92. processFrames(document, document.querySelectorAll(FRAMES_CSS_SELECTOR), message.options, windowId, sessionId);
  93. if (!TOP_WINDOW) {
  94. sendInitResponse({ framesData: [getFrameData(document, window, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
  95. delete document.documentElement.dataset.requestedFrameId;
  96. }
  97. }
  98. function cleanupRequest(message) {
  99. const sessionId = message.sessionId;
  100. const frameElements = document.querySelectorAll(FRAMES_CSS_SELECTOR);
  101. cleanupFrames(frameElements, message.options, message.windowId, sessionId);
  102. }
  103. function initResponse(message) {
  104. const windowData = sessions.get(message.sessionId);
  105. if (windowData) {
  106. if (message.requestedFrameId) {
  107. windowData.requestedFrameId = message.requestedFrameId;
  108. }
  109. message.framesData.forEach(messageFrameData => {
  110. let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
  111. if (!frameData) {
  112. frameData = { windowId: messageFrameData.windowId };
  113. windowData.frames.push(frameData);
  114. }
  115. if (!frameData.processed) {
  116. frameData.content = messageFrameData.content;
  117. frameData.baseURI = messageFrameData.baseURI;
  118. frameData.title = messageFrameData.title;
  119. frameData.stylesheetContents = messageFrameData.stylesheetContents;
  120. frameData.imageData = messageFrameData.imageData;
  121. frameData.postersData = messageFrameData.postersData;
  122. frameData.canvasData = messageFrameData.canvasData;
  123. frameData.fontsData = messageFrameData.fontsData;
  124. frameData.usedFonts = messageFrameData.usedFonts;
  125. frameData.shadowRootContents = messageFrameData.shadowRootContents;
  126. frameData.processed = messageFrameData.processed;
  127. }
  128. });
  129. const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
  130. if (!remainingFrames) {
  131. windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(WINDOW_ID_SEPARATOR).length - frame1.windowId.split(WINDOW_ID_SEPARATOR).length);
  132. if (windowData.resolve) {
  133. if (windowData.requestedFrameId) {
  134. windowData.frames.forEach(frameData => {
  135. if (frameData.windowId == windowData.requestedFrameId) {
  136. frameData.requestedFrame = true;
  137. }
  138. });
  139. }
  140. windowData.resolve(windowData.frames);
  141. }
  142. sessions.delete(message.sessionId);
  143. }
  144. }
  145. }
  146. function processFrames(doc, frameElements, options, parentWindowId, sessionId) {
  147. processFramesAsync(doc, frameElements, options, parentWindowId, sessionId);
  148. if (frameElements.length) {
  149. processFramesSync(doc, frameElements, options, parentWindowId, sessionId);
  150. }
  151. }
  152. function processFramesAsync(doc, frameElements, options, parentWindowId, sessionId) {
  153. const framesData = [];
  154. frameElements.forEach((frameElement, frameIndex) => {
  155. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  156. frameElement.setAttribute(docHelper.WIN_ID_ATTRIBUTE_NAME, windowId);
  157. framesData.push({ windowId });
  158. try {
  159. sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options });
  160. } catch (error) {
  161. /* ignored */
  162. }
  163. setTimeout(() => sendInitResponse({ framesData: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
  164. });
  165. sendInitResponse({ framesData, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
  166. delete document.documentElement.dataset.requestedFrameId;
  167. }
  168. function processFramesSync(doc, frameElements, options, parentWindowId, sessionId) {
  169. const framesData = [];
  170. frameElements.forEach((frameElement, frameIndex) => {
  171. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  172. let frameDoc;
  173. try {
  174. frameDoc = frameElement.contentDocument;
  175. } catch (error) {
  176. // ignored
  177. }
  178. if (frameDoc) {
  179. try {
  180. const frameWindow = frameElement.contentWindow;
  181. frameWindow.stop();
  182. processFrames(frameDoc, frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, windowId, sessionId);
  183. framesData.push(getFrameData(frameDoc, frameWindow, windowId, options));
  184. } catch (error) {
  185. framesData.push({ windowId, processed: true });
  186. }
  187. }
  188. });
  189. sendInitResponse({ framesData, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
  190. delete document.documentElement.dataset.requestedFrameId;
  191. }
  192. function cleanupFrames(frameElements, options, parentWindowId, sessionId) {
  193. frameElements.forEach((frameElement, frameIndex) => {
  194. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  195. frameElement.removeAttribute(docHelper.WIN_ID_ATTRIBUTE_NAME);
  196. try {
  197. sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId, options });
  198. } catch (error) {
  199. /* ignored */
  200. }
  201. });
  202. frameElements.forEach((frameElement, frameIndex) => {
  203. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  204. let frameDoc;
  205. try {
  206. frameDoc = frameElement.contentDocument;
  207. } catch (error) {
  208. // ignored
  209. }
  210. if (frameDoc) {
  211. try {
  212. cleanupFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, windowId, sessionId);
  213. } catch (error) {
  214. // ignored
  215. }
  216. }
  217. });
  218. }
  219. function sendInitResponse(message) {
  220. message.method = INIT_RESPONSE_MESSAGE;
  221. try {
  222. top.frameTree.initResponse(message);
  223. } catch (error) {
  224. sendMessage(top, message, true);
  225. }
  226. }
  227. function sendMessage(targetWindow, message, useChannel) {
  228. if (targetWindow == top && this.browser && browser.runtime && browser.runtime.sendMessage) {
  229. browser.runtime.sendMessage(message);
  230. } else {
  231. if (useChannel) {
  232. const channel = new MessageChannel();
  233. targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method }), TARGET_ORIGIN, [channel.port2]);
  234. channel.port1.postMessage(message);
  235. } else {
  236. targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
  237. }
  238. }
  239. }
  240. function getFrameData(document, window, windowId, options) {
  241. const docData = docHelper.preProcessDoc(document, window, options);
  242. const content = docHelper.serialize(document);
  243. docHelper.postProcessDoc(document, options);
  244. const baseURI = document.baseURI.split("#")[0];
  245. return {
  246. windowId,
  247. content,
  248. baseURI,
  249. title: document.title,
  250. stylesheetContents: docData.stylesheetContents,
  251. imageData: docData.imageData,
  252. postersData: docData.postersData,
  253. canvasData: docData.canvasData,
  254. fontsData: docData.fontsData,
  255. usedFonts: docData.usedFonts,
  256. shadowRootContents: docData.shadowRootContents,
  257. processed: true
  258. };
  259. }
  260. })();