frame-tree.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 TIMEOUT_INIT_REQUEST_MESSAGE = 500;
  24. let sessionId = 0;
  25. const FrameTree = {
  26. getFramesData
  27. };
  28. let sessions = new Map();
  29. addEventListener("message", onFrameWindowMessage, false);
  30. return FrameTree;
  31. async function getFramesData(options) {
  32. options.sessionId = sessionId;
  33. sessionId++;
  34. return new Promise(resolve => {
  35. sessions.set(options.sessionId, {
  36. frames: [],
  37. dataRequestCallbacks: new Map(),
  38. resolve
  39. });
  40. initRequest({ windowId: "0", sessionId: options.sessionId, options });
  41. });
  42. }
  43. function onFrameWindowMessage(event) {
  44. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  45. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  46. if (message.method == "initRequest") {
  47. initRequest(message);
  48. } else if (message.method == "initResponse") {
  49. initResponse(message);
  50. }
  51. }
  52. }
  53. function initRequest(message) {
  54. const windowId = message.windowId;
  55. const sessionId = message.sessionId;
  56. FrameTree.windowId = windowId;
  57. const frameElements = document.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
  58. if (window != top) {
  59. sessions.set(message.sessionId, {
  60. frames: [],
  61. dataRequestCallbacks: new Map()
  62. });
  63. const docData = docHelper.preProcessDoc(document, window, message.options);
  64. const content = docHelper.serialize(document);
  65. docHelper.postProcessDoc(document, window, message.options);
  66. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [{ windowId, content, baseURI: document.baseURI, title: document.title, emptyStyleRulesText: docData.emptyStyleRulesText, canvasData: docData.canvasData, processed: true }], sessionId }), "*");
  67. }
  68. processFrames(frameElements, message.options, windowId, sessionId, window);
  69. }
  70. function initResponse(message) {
  71. if (window == top) {
  72. const sessionFramesData = sessions.get(message.sessionId);
  73. if (sessionFramesData) {
  74. message.framesData.forEach(messageFrameData => {
  75. let frameData = sessionFramesData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
  76. if (!frameData) {
  77. frameData = { windowId: messageFrameData.windowId };
  78. sessionFramesData.frames.push(frameData);
  79. }
  80. frameData.content = messageFrameData.content;
  81. frameData.baseURI = messageFrameData.baseURI;
  82. frameData.title = messageFrameData.title;
  83. frameData.emptyStyleRulesText = messageFrameData.emptyStyleRulesText;
  84. frameData.canvasData = messageFrameData.canvasData;
  85. frameData.processed = messageFrameData.processed;
  86. });
  87. const pendingCount = sessionFramesData.frames.filter(frameData => !frameData.processed).length;
  88. if (!pendingCount) {
  89. sessions.delete(message.sessionId);
  90. sessionFramesData.resolve(sessionFramesData.frames.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length));
  91. }
  92. }
  93. } else {
  94. FrameTree.windowId = message.windowId;
  95. }
  96. }
  97. function processFrames(frameElements, options, windowId, sessionId, win) {
  98. if (win != top) {
  99. win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", windowId, sessionId }), "*");
  100. }
  101. let framesData = [];
  102. frameElements.forEach((frameElement, frameIndex) => {
  103. const frameWinId = windowId + "." + frameIndex;
  104. frameElement.setAttribute(docHelper.WIN_ID_ATTRIBUTE_NAME, frameWinId);
  105. framesData.push({ windowId: frameWinId });
  106. try {
  107. if (!frameElement.contentDocument) {
  108. options.win = null;
  109. frameElement.contentWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, sessionId, frameIndex, options }), "*");
  110. timeout.set(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, sessionId }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
  111. }
  112. } catch (error) {
  113. /* ignored */
  114. }
  115. });
  116. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, sessionId }), "*");
  117. framesData = [];
  118. frameElements.forEach((frameElement, frameIndex) => {
  119. const frameWinId = windowId + "." + frameIndex;
  120. const frameWindow = frameElement.contentWindow;
  121. try {
  122. const frameDoc = frameElement.contentDocument;
  123. if (frameDoc) {
  124. processFrames(frameDoc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"), options, frameWinId, sessionId, frameWindow);
  125. const docData = docHelper.preProcessDoc(frameDoc, frameWindow, options);
  126. framesData.push({ windowId: frameWinId, content: docHelper.serialize(frameDoc), baseURI: frameDoc.baseURI, title: frameDoc.title, emptyStyleRulesText: docData.emptyStyleRulesText, canvasData: docData.canvasData, processed: true });
  127. docHelper.postProcessDoc(frameDoc, frameWindow, options);
  128. }
  129. } catch (error) {
  130. /* ignored */
  131. }
  132. });
  133. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, sessionId }), "*");
  134. }
  135. })();