frame-tree.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, HTMLHtmlElement, addEventListener */
  21. this.FrameTree = (() => {
  22. const browser = this.browser || this.chrome;
  23. const MESSAGE_PREFIX = "__FrameTree__";
  24. const TIMEOUT_INIT_REQUEST_MESSAGE = 1000;
  25. const TIMEOUT_DATA_RESPONSE_MESSAGE = 1000;
  26. const FrameTree = { getFramesData };
  27. let framesData, dataRequestCallbacks;
  28. if (window == top) {
  29. browser.runtime.onMessage.addListener(message => {
  30. if (message.method == "FrameTree.initRequest" && document.documentElement instanceof HTMLHtmlElement) {
  31. dataRequestCallbacks = new Map();
  32. framesData = [];
  33. initRequest(message);
  34. }
  35. if (message.method == "FrameTree.getDataResponse") {
  36. getDataResponse(message);
  37. }
  38. });
  39. }
  40. browser.runtime.onMessage.addListener(message => {
  41. if (message.method == "FrameTree.getDataRequest" && FrameTree.windowId == message.windowId) {
  42. browser.runtime.sendMessage({
  43. method: "FrameTree.getDataResponse",
  44. windowId: message.windowId,
  45. tabId: message.tabId,
  46. content: getDoctype(document) + document.documentElement.outerHTML,
  47. baseURI: document.baseURI,
  48. title: document.title
  49. });
  50. }
  51. });
  52. addEventListener("message", event => {
  53. if (typeof event.data === "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  54. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  55. if (message.method == "initRequest") {
  56. initRequest(message);
  57. } else if (message.method == "initResponse") {
  58. initResponse(message);
  59. } else if (message.method == "getDataResponse") {
  60. getDataResponse(message);
  61. }
  62. }
  63. }, false);
  64. return FrameTree;
  65. async function getFramesData() {
  66. await Promise.all(framesData.map(async frameData => {
  67. return new Promise(resolve => {
  68. dataRequestCallbacks.set(frameData.windowId, resolve);
  69. if (frameData.sameDomain) {
  70. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataRequest", windowId: frameData.windowId }), "*");
  71. } else {
  72. browser.runtime.sendMessage({
  73. method: "FrameTree.getDataRequest",
  74. windowId: frameData.windowId
  75. });
  76. }
  77. frameData.getDataResponseTimeout = setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: frameData.windowId }), "*"), TIMEOUT_DATA_RESPONSE_MESSAGE);
  78. });
  79. }));
  80. return framesData.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length);
  81. }
  82. function initRequest(message) {
  83. FrameTree.windowId = message.windowId;
  84. FrameTree.index = message.index;
  85. const frameElements = document.querySelectorAll("iframe, frame");
  86. if (frameElements.length) {
  87. setFramesWinId(MESSAGE_PREFIX, frameElements, FrameTree.index, FrameTree.windowId, window);
  88. } else {
  89. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: FrameTree.windowId, index: FrameTree.index }), "*");
  90. }
  91. }
  92. function initResponse(message) {
  93. if (window == top) {
  94. if (message.framesData) {
  95. message.framesData = message.framesData instanceof Array ? message.framesData : JSON.parse(message.framesData);
  96. framesData = framesData.concat(message.framesData);
  97. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  98. const pendingCount = framesData.filter(frameData => !frameData.processed).length;
  99. if (message.windowId != "0") {
  100. frameData.processed = true;
  101. }
  102. if (!pendingCount || pendingCount == 1) {
  103. browser.runtime.sendMessage({ method: "FrameTree.initResponse" });
  104. }
  105. }
  106. } else {
  107. FrameTree.windowId = message.windowId;
  108. FrameTree.index = message.index;
  109. }
  110. }
  111. function setFramesWinId(MESSAGE_PREFIX, frameElements, index, windowId, win) {
  112. const framesData = [];
  113. if (win != top) {
  114. win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", windowId, index }), "*");
  115. }
  116. frameElements.forEach((frameElement, index) => {
  117. let src, sameDomain;
  118. try {
  119. sameDomain = Boolean(frameElement.contentDocument && frameElement.contentWindow && top.addEventListener && top);
  120. src = frameElement.src;
  121. } catch (e) {
  122. /* ignored */
  123. }
  124. framesData.push({ sameDomain, src, index, windowId: windowId + "." + index });
  125. });
  126. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, index }), "*");
  127. frameElements.forEach((frameElement, index) => {
  128. const frameWinId = windowId + "." + index;
  129. let frameDoc, frameWindow, topWindow;
  130. try {
  131. frameDoc = frameElement.contentDocument;
  132. frameWindow = frameElement.contentWindow;
  133. topWindow = top.addEventListener && top;
  134. } catch (e) {
  135. /* ignored */
  136. }
  137. if (frameWindow && frameDoc && topWindow) {
  138. setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame"), index, frameWinId, frameWindow);
  139. topWindow.addEventListener("message", onMessage, false);
  140. } else if (frameWindow) {
  141. frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index }), "*");
  142. setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, index }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
  143. }
  144. function onMessage(event) {
  145. if (typeof event.data === "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  146. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  147. if (message.method == "getDataRequest" && message.windowId == frameWinId) {
  148. topWindow.removeEventListener("message", onMessage, false);
  149. const content = getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
  150. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title }), "*");
  151. }
  152. }
  153. }
  154. });
  155. }
  156. function getDataResponse(message) {
  157. delete message.tabId;
  158. delete message.method;
  159. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  160. clearTimeout(frameData.getDataResponseTimeout);
  161. frameData.content = message.content;
  162. frameData.baseURI = message.baseURI;
  163. frameData.title = message.title;
  164. dataRequestCallbacks.get(message.windowId)(message);
  165. }
  166. function getDoctype(doc) {
  167. const docType = doc.doctype;
  168. let docTypeStr;
  169. if (docType) {
  170. docTypeStr = "<!DOCTYPE " + docType.nodeName;
  171. if (docType.publicId) {
  172. docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
  173. if (docType.systemId) {
  174. docTypeStr += " \"" + docType.systemId + "\"";
  175. }
  176. } else if (docType.systemId) {
  177. docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
  178. } if (docType.internalSubset) {
  179. docTypeStr += " [" + docType.internalSubset + "]";
  180. }
  181. return docTypeStr + ">\n";
  182. }
  183. return "";
  184. }
  185. })();