frame-tree.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. }).catch(() => {/* ignored */ });
  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. }).catch(() => {/* ignored */ });
  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. .catch(error => {
  104. //ignored
  105. });
  106. }
  107. }
  108. } else {
  109. FrameTree.windowId = message.windowId;
  110. FrameTree.index = message.index;
  111. }
  112. }
  113. function setFramesWinId(MESSAGE_PREFIX, frameElements, index, windowId, win) {
  114. const framesData = [];
  115. if (win != top) {
  116. win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", windowId, index }), "*");
  117. }
  118. frameElements.forEach((frameElement, index) => {
  119. let src, sameDomain;
  120. try {
  121. sameDomain = Boolean(frameElement.contentDocument && frameElement.contentWindow && top.addEventListener && top);
  122. src = frameElement.src;
  123. } catch (error) {
  124. /* ignored */
  125. }
  126. framesData.push({ sameDomain, src, index, windowId: windowId + "." + index });
  127. });
  128. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, index }), "*");
  129. frameElements.forEach((frameElement, index) => {
  130. const frameWinId = windowId + "." + index;
  131. let frameDoc, frameWindow, topWindow;
  132. try {
  133. frameDoc = frameElement.contentDocument;
  134. frameWindow = frameElement.contentWindow;
  135. topWindow = top.addEventListener && top;
  136. } catch (error) {
  137. /* ignored */
  138. }
  139. if (frameWindow && frameDoc && topWindow) {
  140. setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"), index, frameWinId, frameWindow);
  141. topWindow.addEventListener("message", onMessage, false);
  142. } else if (frameWindow) {
  143. frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index }), "*");
  144. setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, index }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
  145. }
  146. function onMessage(event) {
  147. if (typeof event.data === "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  148. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  149. if (message.method == "getDataRequest" && message.windowId == frameWinId) {
  150. topWindow.removeEventListener("message", onMessage, false);
  151. const content = getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
  152. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title }), "*");
  153. }
  154. }
  155. }
  156. });
  157. }
  158. function getDataResponse(message) {
  159. delete message.tabId;
  160. delete message.method;
  161. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  162. clearTimeout(frameData.getDataResponseTimeout);
  163. frameData.content = message.content;
  164. frameData.baseURI = message.baseURI;
  165. frameData.title = message.title;
  166. dataRequestCallbacks.get(message.windowId)(message);
  167. }
  168. function getDoctype(doc) {
  169. const docType = doc.doctype;
  170. let docTypeStr;
  171. if (docType) {
  172. docTypeStr = "<!DOCTYPE " + docType.nodeName;
  173. if (docType.publicId) {
  174. docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
  175. if (docType.systemId) {
  176. docTypeStr += " \"" + docType.systemId + "\"";
  177. }
  178. } else if (docType.systemId) {
  179. docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
  180. } if (docType.internalSubset) {
  181. docTypeStr += " [" + docType.internalSubset + "]";
  182. }
  183. return docTypeStr + ">\n";
  184. }
  185. return "";
  186. }
  187. })();