frame-tree.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2018 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, timeout */
  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 INIT_RESPONSE_MESSAGE = "initResponse";
  26. const TIMEOUT_INIT_REQUEST_MESSAGE = 500;
  27. const TOP_WINDOW = isTopWindow(window);
  28. let sessions, windowId;
  29. if (TOP_WINDOW) {
  30. sessions = new Map();
  31. }
  32. addEventListener("message", event => {
  33. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  34. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  35. if (message.method == INIT_REQUEST_MESSAGE) {
  36. initRequest(message);
  37. } else if (message.method == INIT_RESPONSE_MESSAGE) {
  38. initResponse(message);
  39. }
  40. }
  41. }, false);
  42. return {
  43. get: async options => {
  44. const sessionId = options.sessionId;
  45. options = JSON.parse(JSON.stringify(options));
  46. options.win = null;
  47. return new Promise(resolve => {
  48. sessions.set(sessionId, { frames: [], resolve });
  49. initRequest({ windowId: "0", sessionId, options });
  50. });
  51. }
  52. };
  53. function initRequest(message) {
  54. const sessionId = message.sessionId;
  55. windowId = message.windowId;
  56. const frameElements = document.querySelectorAll(FRAMES_CSS_SELECTOR);
  57. if (!TOP_WINDOW) {
  58. const docData = docHelper.preProcessDoc(document, window, message.options);
  59. const content = docHelper.serialize(document);
  60. docHelper.postProcessDoc(document, window, message.options);
  61. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({
  62. method: INIT_RESPONSE_MESSAGE, framesData: [{
  63. windowId,
  64. content,
  65. baseURI: document.baseURI,
  66. title: document.title,
  67. emptyStyleRulesText: docData.emptyStyleRulesText,
  68. canvasData: docData.canvasData,
  69. processed: true
  70. }], sessionId
  71. }), "*");
  72. }
  73. processFrames(frameElements, message.options, windowId, sessionId, window);
  74. }
  75. function initResponse(message) {
  76. if (TOP_WINDOW) {
  77. const windowData = sessions.get(message.sessionId);
  78. if (windowData) {
  79. message.framesData.forEach(messageFrameData => {
  80. let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
  81. if (!frameData) {
  82. frameData = { windowId: messageFrameData.windowId };
  83. windowData.frames.push(frameData);
  84. }
  85. frameData.content = messageFrameData.content;
  86. frameData.baseURI = messageFrameData.baseURI;
  87. frameData.title = messageFrameData.title;
  88. frameData.emptyStyleRulesText = messageFrameData.emptyStyleRulesText;
  89. frameData.canvasData = messageFrameData.canvasData;
  90. frameData.processed = messageFrameData.processed;
  91. });
  92. const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
  93. if (!remainingFrames) {
  94. sessions.delete(message.sessionId);
  95. windowData.resolve(windowData.frames.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length));
  96. }
  97. }
  98. } else {
  99. windowId = message.windowId;
  100. }
  101. }
  102. function processFrames(frameElements, options, windowId, sessionId, window) {
  103. if (!isTopWindow(window)) {
  104. window.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE, windowId, sessionId }), "*");
  105. }
  106. let framesData = [];
  107. frameElements.forEach((frameElement, frameIndex) => {
  108. const frameWindowId = windowId + "." + frameIndex;
  109. frameElement.setAttribute(docHelper.windowIdAttributeName(options.sessionId), frameWindowId);
  110. framesData.push({ windowId: frameWindowId });
  111. if (!frameElement.contentDocument) {
  112. try {
  113. frameElement.contentWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_REQUEST_MESSAGE, windowId: frameWindowId, sessionId, options }), "*");
  114. } catch (error) {
  115. /* ignored */
  116. }
  117. }
  118. timeout.set(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({
  119. method: INIT_RESPONSE_MESSAGE,
  120. framesData: [{ windowId: frameWindowId, processed: true }],
  121. windowId: frameWindowId, sessionId
  122. }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
  123. });
  124. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId }), "*");
  125. if (frameElements.length) {
  126. framesData = [];
  127. frameElements.forEach((frameElement, frameIndex) => {
  128. const frameWindowId = windowId + "." + frameIndex;
  129. const frameWindow = frameElement.contentWindow;
  130. const frameDoc = frameElement.contentDocument;
  131. if (frameDoc) {
  132. try {
  133. processFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, frameWindowId, sessionId, frameWindow);
  134. const docData = docHelper.preProcessDoc(frameDoc, frameWindow, options);
  135. framesData.push({
  136. windowId: frameWindowId,
  137. content: docHelper.serialize(frameDoc),
  138. baseURI: frameDoc.baseURI,
  139. title: frameDoc.title,
  140. emptyStyleRulesText: docData.emptyStyleRulesText,
  141. canvasData: docData.canvasData,
  142. processed: true
  143. });
  144. docHelper.postProcessDoc(frameDoc, frameWindow, options);
  145. } catch (error) {
  146. framesData.push({
  147. windowId: frameWindowId,
  148. processed: true
  149. });
  150. }
  151. }
  152. });
  153. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId }), "*");
  154. }
  155. }
  156. function isTopWindow(win) {
  157. return win == top;
  158. }
  159. })();