frame-tree.js 8.6 KB

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