frame-tree.js 5.8 KB

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