frame-tree.js 7.1 KB

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