frame-tree.js 5.8 KB

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