frame-tree.js 7.5 KB

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