frame-tree.js 7.6 KB

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