frame-tree.js 5.7 KB

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