frame-tree.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
  26. const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
  27. const FrameTree = { getFramesData };
  28. let framesData, dataRequestCallbacks;
  29. if (window == top) {
  30. browser.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. browser.runtime.onMessage.addListener(message => {
  42. if (message.method == "FrameTree.getDataRequest" && FrameTree.windowId == message.windowId) {
  43. preProcessDoc(document, window);
  44. browser.runtime.sendMessage({
  45. method: "FrameTree.getDataResponse",
  46. windowId: message.windowId,
  47. tabId: message.tabId,
  48. content: getDoctype(document) + document.documentElement.outerHTML,
  49. emptyStyleRulesText: getEmptyStyleRulesText(document),
  50. canvasData: getCanvasData(document),
  51. baseURI: document.baseURI,
  52. title: document.title
  53. }).catch(() => {/* ignored */ });
  54. postProcessDoc(document);
  55. }
  56. });
  57. addEventListener("message", event => {
  58. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  59. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  60. if (message.method == "initRequest") {
  61. initRequest(message);
  62. } else if (message.method == "initResponse") {
  63. initResponse(message);
  64. } else if (message.method == "getDataResponse") {
  65. getDataResponse(message);
  66. }
  67. }
  68. }, false);
  69. return FrameTree;
  70. async function getFramesData() {
  71. await Promise.all(framesData.map(async frameData => {
  72. return new Promise(resolve => {
  73. dataRequestCallbacks.set(frameData.windowId, resolve);
  74. if (frameData.sameDomain) {
  75. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataRequest", windowId: frameData.windowId }), "*");
  76. } else {
  77. browser.runtime.sendMessage({
  78. method: "FrameTree.getDataRequest",
  79. windowId: frameData.windowId
  80. }).catch(() => { /* ignored */ });
  81. }
  82. frameData.getDataResponseTimeout = setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: frameData.windowId }), "*"), TIMEOUT_DATA_RESPONSE_MESSAGE);
  83. });
  84. }));
  85. return framesData.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length);
  86. }
  87. function initRequest(message) {
  88. FrameTree.windowId = message.windowId;
  89. FrameTree.index = message.index;
  90. const frameElements = document.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
  91. if (frameElements.length) {
  92. setFramesWinId(MESSAGE_PREFIX, frameElements, FrameTree.index, FrameTree.windowId, window);
  93. } else {
  94. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: FrameTree.windowId, index: FrameTree.index }), "*");
  95. }
  96. }
  97. function initResponse(message) {
  98. if (window == top) {
  99. if (message.framesData) {
  100. message.framesData = message.framesData instanceof Array ? message.framesData : JSON.parse(message.framesData);
  101. framesData = framesData.concat(message.framesData);
  102. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  103. const pendingCount = framesData.filter(frameData => !frameData.processed).length;
  104. if (message.windowId != "0") {
  105. frameData.processed = true;
  106. }
  107. if (!pendingCount || pendingCount == 1) {
  108. browser.runtime.sendMessage({ method: "FrameTree.initResponse" })
  109. .catch(() => { /* ignored */ });
  110. }
  111. }
  112. } else {
  113. FrameTree.windowId = message.windowId;
  114. FrameTree.index = message.index;
  115. }
  116. }
  117. function setFramesWinId(MESSAGE_PREFIX, frameElements, index, windowId, win) {
  118. const framesData = [];
  119. if (win != top) {
  120. win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", windowId, index }), "*");
  121. }
  122. frameElements.forEach((frameElement, index) => {
  123. let src, sameDomain;
  124. try {
  125. sameDomain = Boolean(frameElement.contentDocument && frameElement.contentWindow && top.addEventListener && top);
  126. src = frameElement.src;
  127. } catch (error) {
  128. /* ignored */
  129. }
  130. framesData.push({ sameDomain, src, index, windowId: windowId + "." + index });
  131. });
  132. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, index }), "*");
  133. frameElements.forEach((frameElement, index) => {
  134. const frameWinId = windowId + "." + index;
  135. frameElement.setAttribute("data-frame-tree-win-id", frameWinId);
  136. let frameDoc, frameWindow, topWindow;
  137. try {
  138. frameDoc = frameElement.contentDocument;
  139. frameWindow = frameElement.contentWindow;
  140. topWindow = top.addEventListener && top;
  141. } catch (error) {
  142. /* ignored */
  143. }
  144. if (frameWindow && frameDoc && topWindow) {
  145. setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"), index, frameWinId, frameWindow);
  146. topWindow.addEventListener("message", onMessage, false);
  147. } else if (frameWindow) {
  148. frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index }), "*");
  149. setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, index }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
  150. }
  151. function onMessage(event) {
  152. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  153. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  154. if (message.method == "getDataRequest" && message.windowId == frameWinId) {
  155. topWindow.removeEventListener("message", onMessage, false);
  156. const content = getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
  157. const emptyStyleRulesText = getEmptyStyleRulesText(frameDoc);
  158. const canvasData = getCanvasData(frameDoc);
  159. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title, emptyStyleRulesText, canvasData }), "*");
  160. }
  161. }
  162. }
  163. });
  164. }
  165. function getDataResponse(message) {
  166. delete message.tabId;
  167. delete message.method;
  168. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  169. clearTimeout(frameData.getDataResponseTimeout);
  170. frameData.content = message.content;
  171. frameData.baseURI = message.baseURI;
  172. frameData.title = message.title;
  173. frameData.emptyStyleRulesText = message.emptyStyleRulesText;
  174. frameData.canvasData = message.canvasData;
  175. dataRequestCallbacks.get(message.windowId)(message);
  176. }
  177. function getDoctype(doc) {
  178. const docType = doc.doctype;
  179. let docTypeStr;
  180. if (docType) {
  181. docTypeStr = "<!DOCTYPE " + docType.nodeName;
  182. if (docType.publicId) {
  183. docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
  184. if (docType.systemId) {
  185. docTypeStr += " \"" + docType.systemId + "\"";
  186. }
  187. } else if (docType.systemId) {
  188. docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
  189. } if (docType.internalSubset) {
  190. docTypeStr += " [" + docType.internalSubset + "]";
  191. }
  192. return docTypeStr + ">\n";
  193. }
  194. return "";
  195. }
  196. function getEmptyStyleRulesText(doc) {
  197. if (doc) {
  198. const textData = [];
  199. doc.querySelectorAll("style").forEach(styleElement => {
  200. if (!styleElement.textContent) {
  201. textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
  202. }
  203. });
  204. return textData;
  205. }
  206. }
  207. function getCanvasData(doc) {
  208. if (doc) {
  209. const canvasData = [];
  210. doc.querySelectorAll("canvas").forEach(canvasElement => {
  211. try {
  212. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  213. } catch (error) {
  214. canvasData.push(null);
  215. }
  216. });
  217. return canvasData;
  218. }
  219. }
  220. function preProcessDoc(doc, win) {
  221. doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  222. doc.head.querySelectorAll("noscript").forEach(element => {
  223. const disabledNoscriptElement = doc.createElement("disabled-noscript");
  224. Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
  225. disabledNoscriptElement.hidden = true;
  226. element.parentElement.replaceChild(disabledNoscriptElement, element);
  227. });
  228. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
  229. doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object)").forEach(element => {
  230. const style = win.getComputedStyle(element);
  231. if (element instanceof win.HTMLElement && (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) && !element.querySelector("iframe, frame, object[type=\"text/html\"][data]")) {
  232. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  233. }
  234. });
  235. doc.querySelectorAll("*").forEach(element => {
  236. const style = win.getComputedStyle(element);
  237. if (style.whiteSpace.startsWith("pre")) {
  238. element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  239. }
  240. });
  241. }
  242. function postProcessDoc(doc) {
  243. doc.head.querySelectorAll("disabled-noscript").forEach(element => {
  244. const noscriptElement = this.options.doc.createElement("noscript");
  245. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  246. element.parentElement.replaceChild(noscriptElement, element);
  247. });
  248. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  249. doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
  250. doc.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
  251. }
  252. })();