frame-tree.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, message.options);
  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, message.options);
  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(options) {
  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, options: { removeHiddenElements: options.removeHiddenElements, compressHTML: options.compressHTML } }), "*");
  76. } else {
  77. browser.runtime.sendMessage({
  78. method: "FrameTree.getDataRequest",
  79. windowId: frameData.windowId,
  80. options: { removeHiddenElements: options.removeHiddenElements, compressHTML: options.compressHTML }
  81. }).catch(() => { /* ignored */ });
  82. }
  83. frameData.getDataResponseTimeout = setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: frameData.windowId }), "*"), TIMEOUT_DATA_RESPONSE_MESSAGE);
  84. });
  85. }));
  86. return framesData.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length);
  87. }
  88. function initRequest(message) {
  89. FrameTree.windowId = message.windowId;
  90. FrameTree.index = message.index;
  91. const frameElements = document.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
  92. if (frameElements.length) {
  93. setFramesWinId(MESSAGE_PREFIX, frameElements, FrameTree.index, FrameTree.windowId, window);
  94. } else {
  95. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: FrameTree.windowId, index: FrameTree.index }), "*");
  96. }
  97. }
  98. function initResponse(message) {
  99. if (window == top) {
  100. if (message.framesData) {
  101. message.framesData = message.framesData instanceof Array ? message.framesData : JSON.parse(message.framesData);
  102. framesData = framesData.concat(message.framesData);
  103. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  104. const pendingCount = framesData.filter(frameData => !frameData.processed).length;
  105. if (message.windowId != "0") {
  106. frameData.processed = true;
  107. }
  108. if (!pendingCount || pendingCount == 1) {
  109. browser.runtime.sendMessage({ method: "FrameTree.initResponse" })
  110. .catch(() => { /* ignored */ });
  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({ method: "initResponse", 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 (error) {
  129. /* ignored */
  130. }
  131. framesData.push({ sameDomain, src, index, windowId: windowId + "." + index });
  132. });
  133. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, index }), "*");
  134. frameElements.forEach((frameElement, index) => {
  135. const frameWinId = windowId + "." + index;
  136. frameElement.setAttribute("data-frame-tree-win-id", frameWinId);
  137. let frameDoc, frameWindow, topWindow;
  138. try {
  139. frameDoc = frameElement.contentDocument;
  140. frameWindow = frameElement.contentWindow;
  141. topWindow = top.addEventListener && top;
  142. } catch (error) {
  143. /* ignored */
  144. }
  145. if (frameWindow && frameDoc && topWindow) {
  146. setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"), index, frameWinId, frameWindow);
  147. topWindow.addEventListener("message", onMessage, false);
  148. } else if (frameWindow) {
  149. frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index }), "*");
  150. setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, index }), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
  151. }
  152. function onMessage(event) {
  153. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
  154. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
  155. if (message.method == "getDataRequest" && message.windowId == frameWinId) {
  156. topWindow.removeEventListener("message", onMessage, false);
  157. preProcessDoc(frameDoc, frameWindow, message.options);
  158. const content = getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
  159. const emptyStyleRulesText = getEmptyStyleRulesText(frameDoc);
  160. const canvasData = getCanvasData(frameDoc);
  161. top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title, emptyStyleRulesText, canvasData }), "*");
  162. postProcessDoc(frameDoc, frameWindow, message.options);
  163. }
  164. }
  165. }
  166. });
  167. }
  168. function getDataResponse(message) {
  169. delete message.tabId;
  170. delete message.method;
  171. const frameData = framesData.find(frameData => frameData.windowId == message.windowId);
  172. clearTimeout(frameData.getDataResponseTimeout);
  173. frameData.content = message.content;
  174. frameData.baseURI = message.baseURI;
  175. frameData.title = message.title;
  176. frameData.emptyStyleRulesText = message.emptyStyleRulesText;
  177. frameData.canvasData = message.canvasData;
  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. function getEmptyStyleRulesText(doc) {
  200. if (doc) {
  201. const textData = [];
  202. doc.querySelectorAll("style").forEach(styleElement => {
  203. if (!styleElement.textContent) {
  204. textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
  205. }
  206. });
  207. return textData;
  208. }
  209. }
  210. function getCanvasData(doc) {
  211. if (doc) {
  212. const canvasData = [];
  213. doc.querySelectorAll("canvas").forEach(canvasElement => {
  214. try {
  215. canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
  216. } catch (error) {
  217. canvasData.push(null);
  218. }
  219. });
  220. return canvasData;
  221. }
  222. }
  223. function preProcessDoc(doc, win, options) {
  224. doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
  225. doc.head.querySelectorAll("noscript").forEach(element => {
  226. const disabledNoscriptElement = doc.createElement("disabled-noscript");
  227. Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
  228. disabledNoscriptElement.hidden = true;
  229. element.parentElement.replaceChild(disabledNoscriptElement, element);
  230. });
  231. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
  232. if (options.removeHiddenElements) {
  233. doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object)").forEach(element => {
  234. const style = win.getComputedStyle(element);
  235. 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]")) {
  236. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  237. }
  238. });
  239. }
  240. if (options.compressHTML) {
  241. doc.querySelectorAll("*").forEach(element => {
  242. const style = win.getComputedStyle(element);
  243. if (style.whiteSpace.startsWith("pre")) {
  244. element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  245. }
  246. });
  247. }
  248. }
  249. function postProcessDoc(doc, options) {
  250. doc.head.querySelectorAll("disabled-noscript").forEach(element => {
  251. const noscriptElement = doc.createElement("noscript");
  252. Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
  253. element.parentElement.replaceChild(noscriptElement, element);
  254. });
  255. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  256. if (options.removeHiddenElements) {
  257. doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
  258. }
  259. if (options.compressHTML) {
  260. doc.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
  261. }
  262. }
  263. })();