single-file-frames.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.singlefile = {}));
  5. })(this, (function (exports) { 'use strict';
  6. /*
  7. * Copyright 2010-2022 Gildas Lormeau
  8. * contact : gildas.lormeau <at> gmail.com
  9. *
  10. * This file is part of SingleFile.
  11. *
  12. * The code in this file is free software: you can redistribute it and/or
  13. * modify it under the terms of the GNU Affero General Public License
  14. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  15. * of the License, or (at your option) any later version.
  16. *
  17. * The code in this file is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  20. * General Public License for more details.
  21. *
  22. * As additional permission under GNU AGPL version 3 section 7, you may
  23. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  24. * AGPL normally required by section 4, provided you include this license
  25. * notice and a URL through which recipients can access the Corresponding
  26. * Source.
  27. */
  28. /* global globalThis, window */
  29. const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
  30. const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
  31. const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT = "single-file-load-deferred-images-keep-zoom-level-start";
  32. const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT = "single-file-load-deferred-images-keep-zoom-level-end";
  33. const BLOCK_COOKIES_START_EVENT = "single-file-block-cookies-start";
  34. const BLOCK_COOKIES_END_EVENT = "single-file-block-cookies-end";
  35. const DISPATCH_SCROLL_START_EVENT = "single-file-dispatch-scroll-event-start";
  36. const DISPATCH_SCROLL_END_EVENT = "single-file-dispatch-scroll-event-end";
  37. const BLOCK_STORAGE_START_EVENT = "single-file-block-storage-start";
  38. const BLOCK_STORAGE_END_EVENT = "single-file-block-storage-end";
  39. const LOAD_IMAGE_EVENT = "single-file-load-image";
  40. const IMAGE_LOADED_EVENT = "single-file-image-loaded";
  41. const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
  42. const DELETE_FONT_EVENT = "single-file-delete-font";
  43. const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
  44. const FONT_FACE_PROPERTY_NAME = "_singleFile_fontFaces";
  45. const addEventListener$2 = (type, listener, options) => globalThis.addEventListener(type, listener, options);
  46. const dispatchEvent = event => { try { globalThis.dispatchEvent(event); } catch (error) { /* ignored */ } };
  47. const CustomEvent$1 = globalThis.CustomEvent;
  48. const document$2 = globalThis.document;
  49. const Document = globalThis.Document;
  50. const JSON$2 = globalThis.JSON;
  51. let fontFaces;
  52. if (window[FONT_FACE_PROPERTY_NAME]) {
  53. fontFaces = window[FONT_FACE_PROPERTY_NAME];
  54. } else {
  55. fontFaces = window[FONT_FACE_PROPERTY_NAME] = new Map();
  56. }
  57. if (document$2 instanceof Document) {
  58. addEventListener$2(NEW_FONT_FACE_EVENT, event => {
  59. const detail = event.detail;
  60. const key = Object.assign({}, detail);
  61. delete key.src;
  62. fontFaces.set(JSON$2.stringify(key), detail);
  63. });
  64. addEventListener$2(DELETE_FONT_EVENT, event => {
  65. const detail = event.detail;
  66. const key = Object.assign({}, detail);
  67. delete key.src;
  68. fontFaces.delete(JSON$2.stringify(key));
  69. });
  70. addEventListener$2(CLEAR_FONTS_EVENT, () => fontFaces = new Map());
  71. }
  72. function getFontsData$1() {
  73. return Array.from(fontFaces.values());
  74. }
  75. function loadDeferredImagesStart(options) {
  76. if (options.loadDeferredImagesBlockCookies) {
  77. dispatchEvent(new CustomEvent$1(BLOCK_COOKIES_START_EVENT));
  78. }
  79. if (options.loadDeferredImagesBlockStorage) {
  80. dispatchEvent(new CustomEvent$1(BLOCK_STORAGE_START_EVENT));
  81. }
  82. if (options.loadDeferredImagesDispatchScrollEvent) {
  83. dispatchEvent(new CustomEvent$1(DISPATCH_SCROLL_START_EVENT));
  84. }
  85. if (options.loadDeferredImagesKeepZoomLevel) {
  86. dispatchEvent(new CustomEvent$1(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT));
  87. } else {
  88. dispatchEvent(new CustomEvent$1(LOAD_DEFERRED_IMAGES_START_EVENT));
  89. }
  90. }
  91. function loadDeferredImagesEnd(options) {
  92. if (options.loadDeferredImagesBlockCookies) {
  93. dispatchEvent(new CustomEvent$1(BLOCK_COOKIES_END_EVENT));
  94. }
  95. if (options.loadDeferredImagesBlockStorage) {
  96. dispatchEvent(new CustomEvent$1(BLOCK_STORAGE_END_EVENT));
  97. }
  98. if (options.loadDeferredImagesDispatchScrollEvent) {
  99. dispatchEvent(new CustomEvent$1(DISPATCH_SCROLL_END_EVENT));
  100. }
  101. if (options.loadDeferredImagesKeepZoomLevel) {
  102. dispatchEvent(new CustomEvent$1(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT));
  103. } else {
  104. dispatchEvent(new CustomEvent$1(LOAD_DEFERRED_IMAGES_END_EVENT));
  105. }
  106. }
  107. /*
  108. * The MIT License (MIT)
  109. *
  110. * Author: Gildas Lormeau
  111. *
  112. * Permission is hereby granted, free of charge, to any person obtaining a copy
  113. * of this software and associated documentation files (the "Software"), to deal
  114. * in the Software without restriction, including without limitation the rights
  115. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  116. * copies of the Software, and to permit persons to whom the Software is
  117. * furnished to do so, subject to the following conditions:
  118. *
  119. * The above copyright notice and this permission notice shall be included in all
  120. * copies or substantial portions of the Software.
  121. *
  122. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  123. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  124. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  125. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  126. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  127. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  128. * SOFTWARE.
  129. */
  130. // derived from https://github.com/postcss/postcss-selector-parser/blob/master/src/util/unesc.js
  131. /*
  132. * The MIT License (MIT)
  133. * Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
  134. *
  135. * Permission is hereby granted, free of charge, to any person obtaining a copy
  136. * of this software and associated documentation files (the "Software"), to deal
  137. * in the Software without restriction, including without limitation the rights
  138. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  139. * copies of the Software, and to permit persons to whom the Software is
  140. * furnished to do so, subject to the following conditions:
  141. *
  142. * The above copyright notice and this permission notice shall be included in
  143. * all copies or substantial portions of the Software.
  144. *
  145. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  146. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  147. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  148. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  149. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  150. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  151. * THE SOFTWARE.
  152. */
  153. const whitespace = "[\\x20\\t\\r\\n\\f]";
  154. const unescapeRegExp = new RegExp("\\\\([\\da-f]{1,6}" + whitespace + "?|(" + whitespace + ")|.)", "ig");
  155. function process$1(str) {
  156. return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
  157. const high = "0x" + escaped - 0x10000;
  158. // NaN means non-codepoint
  159. // Workaround erroneous numeric interpretation of +"0x"
  160. // eslint-disable-next-line no-self-compare
  161. return high !== high || escapedWhitespace
  162. ? escaped
  163. : high < 0
  164. ? // BMP codepoint
  165. String.fromCharCode(high + 0x10000)
  166. : // Supplemental Plane codepoint (surrogate pair)
  167. String.fromCharCode((high >> 10) | 0xd800, (high & 0x3ff) | 0xdc00);
  168. });
  169. }
  170. const SINGLE_FILE_PREFIX = "single-file-";
  171. const WAIT_FOR_USERSCRIPT_PROPERTY_NAME = "_singleFile_waitForUserScript";
  172. const MESSAGE_PREFIX = "__frameTree__::";
  173. /*
  174. * Copyright 2010-2022 Gildas Lormeau
  175. * contact : gildas.lormeau <at> gmail.com
  176. *
  177. * This file is part of SingleFile.
  178. *
  179. * The code in this file is free software: you can redistribute it and/or
  180. * modify it under the terms of the GNU Affero General Public License
  181. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  182. * of the License, or (at your option) any later version.
  183. *
  184. * The code in this file is distributed in the hope that it will be useful,
  185. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  186. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  187. * General Public License for more details.
  188. *
  189. * As additional permission under GNU AGPL version 3 section 7, you may
  190. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  191. * AGPL normally required by section 4, provided you include this license
  192. * notice and a URL through which recipients can access the Corresponding
  193. * Source.
  194. */
  195. const ON_BEFORE_CAPTURE_EVENT_NAME = SINGLE_FILE_PREFIX + "on-before-capture";
  196. const ON_AFTER_CAPTURE_EVENT_NAME = SINGLE_FILE_PREFIX + "on-after-capture";
  197. const GET_ADOPTED_STYLESHEETS_REQUEST_EVENT = SINGLE_FILE_PREFIX + "request-get-adopted-stylesheets";
  198. const GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT = SINGLE_FILE_PREFIX + "response-get-adopted-stylesheets";
  199. const UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT = SINGLE_FILE_PREFIX + "unregister-request-get-adopted-stylesheets";
  200. const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "removed-content";
  201. const HIDDEN_CONTENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "hidden-content";
  202. const KEPT_CONTENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "kept-content";
  203. const HIDDEN_FRAME_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "hidden-frame";
  204. const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "preserved-space-element";
  205. const SHADOW_ROOT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "shadow-root-element";
  206. const WIN_ID_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "win-id";
  207. const IMAGE_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "image";
  208. const POSTER_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "poster";
  209. const VIDEO_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "video";
  210. const CANVAS_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "canvas";
  211. const STYLE_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "movable-style";
  212. const INPUT_VALUE_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "input-value";
  213. const LAZY_SRC_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "lazy-loaded-src";
  214. const STYLESHEET_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "stylesheet";
  215. const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "disabled-noscript";
  216. const INVALID_ELEMENT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "invalid-element";
  217. const ASYNC_SCRIPT_ATTRIBUTE_NAME = "data-" + SINGLE_FILE_PREFIX + "async-script";
  218. const FLOW_ELEMENTS_SELECTOR = "*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)";
  219. const KEPT_TAG_NAMES = ["NOSCRIPT", "DISABLED-NOSCRIPT", "META", "LINK", "STYLE", "TITLE", "TEMPLATE", "SOURCE", "OBJECT", "SCRIPT", "HEAD", "BODY"];
  220. const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
  221. const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
  222. const FONT_WEIGHTS = {
  223. regular: "400",
  224. normal: "400",
  225. bold: "700",
  226. bolder: "700",
  227. lighter: "100"
  228. };
  229. const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
  230. const EMPTY_RESOURCE = "data:,";
  231. const JSON$1 = globalThis.JSON;
  232. function initDoc(doc) {
  233. doc.querySelectorAll("meta[http-equiv=refresh]").forEach(element => {
  234. element.removeAttribute("http-equiv");
  235. element.setAttribute("disabled-http-equiv", "refresh");
  236. });
  237. }
  238. function preProcessDoc(doc, win, options) {
  239. doc.querySelectorAll("noscript:not([" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "])").forEach(element => {
  240. element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.textContent);
  241. element.textContent = "";
  242. });
  243. initDoc(doc);
  244. if (doc.head) {
  245. doc.head.querySelectorAll(FLOW_ELEMENTS_SELECTOR).forEach(element => element.hidden = true);
  246. }
  247. doc.querySelectorAll("svg foreignObject").forEach(element => {
  248. const flowElements = element.querySelectorAll("html > head > " + FLOW_ELEMENTS_SELECTOR + ", html > body > " + FLOW_ELEMENTS_SELECTOR);
  249. if (flowElements.length) {
  250. Array.from(element.childNodes).forEach(node => node.remove());
  251. flowElements.forEach(flowElement => element.appendChild(flowElement));
  252. }
  253. });
  254. const invalidElements = new Map();
  255. let elementsInfo;
  256. if (win && doc.documentElement) {
  257. doc.querySelectorAll("button button, a a").forEach(element => {
  258. const placeHolderElement = doc.createElement("template");
  259. placeHolderElement.setAttribute(INVALID_ELEMENT_ATTRIBUTE_NAME, "");
  260. placeHolderElement.content.appendChild(element.cloneNode(true));
  261. invalidElements.set(element, placeHolderElement);
  262. element.replaceWith(placeHolderElement);
  263. });
  264. elementsInfo = getElementsInfo(win, doc, doc.documentElement, options);
  265. if (options.moveStylesInHead) {
  266. doc.querySelectorAll("body style, body ~ style").forEach(element => {
  267. const computedStyle = getComputedStyle(win, element);
  268. if (computedStyle && testHiddenElement(element, computedStyle)) {
  269. element.setAttribute(STYLE_ATTRIBUTE_NAME, "");
  270. elementsInfo.markedElements.push(element);
  271. }
  272. });
  273. }
  274. } else {
  275. elementsInfo = {
  276. canvases: [],
  277. images: [],
  278. posters: [],
  279. videos: [],
  280. usedFonts: [],
  281. shadowRoots: [],
  282. markedElements: []
  283. };
  284. }
  285. return {
  286. canvases: elementsInfo.canvases,
  287. fonts: getFontsData(),
  288. stylesheets: getStylesheetsData(doc),
  289. images: elementsInfo.images,
  290. posters: elementsInfo.posters,
  291. videos: elementsInfo.videos,
  292. usedFonts: Array.from(elementsInfo.usedFonts.values()),
  293. shadowRoots: elementsInfo.shadowRoots,
  294. referrer: doc.referrer,
  295. markedElements: elementsInfo.markedElements,
  296. invalidElements,
  297. scrollPosition: { x: win.scrollX, y: win.scrollY },
  298. adoptedStyleSheets: getStylesheetsContent(doc.adoptedStyleSheets)
  299. };
  300. }
  301. function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map(), canvases: [], images: [], posters: [], videos: [], shadowRoots: [], markedElements: [] }, ascendantHidden) {
  302. if (element.childNodes) {
  303. const elements = Array.from(element.childNodes).filter(node => (node instanceof win.HTMLElement) || (node instanceof win.SVGElement));
  304. elements.forEach(element => {
  305. let elementHidden, elementKept, computedStyle;
  306. if (!options.autoSaveExternalSave && (options.removeHiddenElements || options.removeUnusedFonts || options.compressHTML)) {
  307. computedStyle = getComputedStyle(win, element);
  308. if (element instanceof win.HTMLElement) {
  309. if (options.removeHiddenElements) {
  310. elementKept = ((ascendantHidden || element.closest("html > head")) && KEPT_TAG_NAMES.includes(element.tagName.toUpperCase())) || element.closest("details");
  311. if (!elementKept) {
  312. elementHidden = ascendantHidden || testHiddenElement(element, computedStyle);
  313. if (elementHidden) {
  314. element.setAttribute(HIDDEN_CONTENT_ATTRIBUTE_NAME, "");
  315. data.markedElements.push(element);
  316. }
  317. }
  318. }
  319. }
  320. if (!elementHidden) {
  321. if (options.compressHTML && computedStyle) {
  322. const whiteSpace = computedStyle.getPropertyValue("white-space");
  323. if (whiteSpace && whiteSpace.startsWith("pre")) {
  324. element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
  325. data.markedElements.push(element);
  326. }
  327. }
  328. if (options.removeUnusedFonts) {
  329. getUsedFont(computedStyle, options, data.usedFonts);
  330. getUsedFont(getComputedStyle(win, element, ":first-letter"), options, data.usedFonts);
  331. getUsedFont(getComputedStyle(win, element, ":before"), options, data.usedFonts);
  332. getUsedFont(getComputedStyle(win, element, ":after"), options, data.usedFonts);
  333. }
  334. }
  335. }
  336. getResourcesInfo(win, doc, element, options, data, elementHidden, computedStyle);
  337. const shadowRoot = !(element instanceof win.SVGElement) && getShadowRoot(element);
  338. if (shadowRoot && !element.classList.contains(SINGLE_FILE_UI_ELEMENT_CLASS)) {
  339. const shadowRootInfo = {};
  340. element.setAttribute(SHADOW_ROOT_ATTRIBUTE_NAME, data.shadowRoots.length);
  341. data.markedElements.push(element);
  342. data.shadowRoots.push(shadowRootInfo);
  343. try {
  344. if (shadowRoot.adoptedStyleSheets) {
  345. if (shadowRoot.adoptedStyleSheets.length) {
  346. shadowRootInfo.adoptedStyleSheets = getStylesheetsContent(shadowRoot.adoptedStyleSheets);
  347. } else if (shadowRoot.adoptedStyleSheets.length === undefined) {
  348. const listener = event => shadowRootInfo.adoptedStyleSheets = event.detail.adoptedStyleSheets;
  349. element.addEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
  350. element.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
  351. element.removeEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
  352. }
  353. }
  354. } catch (error) {
  355. // ignored
  356. }
  357. getElementsInfo(win, doc, shadowRoot, options, data, elementHidden);
  358. shadowRootInfo.content = shadowRoot.innerHTML;
  359. shadowRootInfo.mode = shadowRoot.mode;
  360. try {
  361. if (shadowRoot.adoptedStyleSheets && shadowRoot.adoptedStyleSheets.length === undefined) {
  362. element.dispatchEvent(new CustomEvent(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
  363. }
  364. } catch (error) {
  365. // ignored
  366. }
  367. }
  368. getElementsInfo(win, doc, element, options, data, elementHidden);
  369. if (!options.autoSaveExternalSave && options.removeHiddenElements && ascendantHidden) {
  370. if (elementKept || element.getAttribute(KEPT_CONTENT_ATTRIBUTE_NAME) == "") {
  371. if (element.parentElement) {
  372. element.parentElement.setAttribute(KEPT_CONTENT_ATTRIBUTE_NAME, "");
  373. data.markedElements.push(element.parentElement);
  374. }
  375. } else if (elementHidden) {
  376. element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
  377. data.markedElements.push(element);
  378. }
  379. }
  380. });
  381. }
  382. return data;
  383. }
  384. function getStylesheetsContent(styleSheets) {
  385. return styleSheets ? Array.from(styleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n")) : [];
  386. }
  387. function getResourcesInfo(win, doc, element, options, data, elementHidden, computedStyle) {
  388. const tagName = element.tagName && element.tagName.toUpperCase();
  389. if (tagName == "CANVAS") {
  390. try {
  391. data.canvases.push({
  392. dataURI: element.toDataURL("image/png", ""),
  393. backgroundColor: computedStyle.getPropertyValue("background-color")
  394. });
  395. element.setAttribute(CANVAS_ATTRIBUTE_NAME, data.canvases.length - 1);
  396. data.markedElements.push(element);
  397. } catch (error) {
  398. // ignored
  399. }
  400. }
  401. if (tagName == "IMG") {
  402. const imageData = {
  403. currentSrc: elementHidden ?
  404. EMPTY_RESOURCE :
  405. (options.loadDeferredImages && element.getAttribute(LAZY_SRC_ATTRIBUTE_NAME)) || element.currentSrc
  406. };
  407. data.images.push(imageData);
  408. element.setAttribute(IMAGE_ATTRIBUTE_NAME, data.images.length - 1);
  409. data.markedElements.push(element);
  410. element.removeAttribute(LAZY_SRC_ATTRIBUTE_NAME);
  411. computedStyle = computedStyle || getComputedStyle(win, element);
  412. if (computedStyle) {
  413. imageData.size = getSize(win, element, computedStyle);
  414. const boxShadow = computedStyle.getPropertyValue("box-shadow");
  415. const backgroundImage = computedStyle.getPropertyValue("background-image");
  416. if ((!boxShadow || boxShadow == "none") &&
  417. (!backgroundImage || backgroundImage == "none") &&
  418. (imageData.size.pxWidth > 1 || imageData.size.pxHeight > 1)) {
  419. imageData.replaceable = true;
  420. imageData.backgroundColor = computedStyle.getPropertyValue("background-color");
  421. imageData.objectFit = computedStyle.getPropertyValue("object-fit");
  422. imageData.boxSizing = computedStyle.getPropertyValue("box-sizing");
  423. imageData.objectPosition = computedStyle.getPropertyValue("object-position");
  424. }
  425. }
  426. }
  427. if (tagName == "VIDEO") {
  428. const src = element.currentSrc;
  429. if (src && !src.startsWith("blob:") && !src.startsWith("data:")) {
  430. const computedStyle = getComputedStyle(win, element.parentNode);
  431. data.videos.push({
  432. positionParent: computedStyle && computedStyle.getPropertyValue("position"),
  433. src,
  434. size: {
  435. pxWidth: element.clientWidth,
  436. pxHeight: element.clientHeight
  437. },
  438. currentTime: element.currentTime
  439. });
  440. element.setAttribute(VIDEO_ATTRIBUTE_NAME, data.videos.length - 1);
  441. }
  442. if (!element.getAttribute("poster")) {
  443. const canvasElement = doc.createElement("canvas");
  444. const context = canvasElement.getContext("2d");
  445. canvasElement.width = element.clientWidth;
  446. canvasElement.height = element.clientHeight;
  447. try {
  448. context.drawImage(element, 0, 0, canvasElement.width, canvasElement.height);
  449. data.posters.push(canvasElement.toDataURL("image/png", ""));
  450. element.setAttribute(POSTER_ATTRIBUTE_NAME, data.posters.length - 1);
  451. data.markedElements.push(element);
  452. } catch (error) {
  453. // ignored
  454. }
  455. }
  456. }
  457. if (tagName == "IFRAME") {
  458. if (elementHidden && options.removeHiddenElements) {
  459. element.setAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME, "");
  460. data.markedElements.push(element);
  461. }
  462. }
  463. if (tagName == "INPUT") {
  464. if (element.type != "password") {
  465. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
  466. data.markedElements.push(element);
  467. }
  468. if (element.type == "radio" || element.type == "checkbox") {
  469. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.checked);
  470. data.markedElements.push(element);
  471. }
  472. }
  473. if (tagName == "TEXTAREA") {
  474. element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
  475. data.markedElements.push(element);
  476. }
  477. if (tagName == "SELECT") {
  478. element.querySelectorAll("option").forEach(option => {
  479. if (option.selected) {
  480. option.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, "");
  481. data.markedElements.push(option);
  482. }
  483. });
  484. }
  485. if (tagName == "SCRIPT") {
  486. if (element.async && element.getAttribute("async") != "" && element.getAttribute("async") != "async") {
  487. element.setAttribute(ASYNC_SCRIPT_ATTRIBUTE_NAME, "");
  488. data.markedElements.push(element);
  489. }
  490. element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>");
  491. }
  492. }
  493. function getUsedFont(computedStyle, options, usedFonts) {
  494. if (computedStyle) {
  495. const fontStyle = computedStyle.getPropertyValue("font-style") || "normal";
  496. computedStyle.getPropertyValue("font-family").split(",").forEach(fontFamilyName => {
  497. fontFamilyName = normalizeFontFamily(fontFamilyName);
  498. if (!options.loadedFonts || options.loadedFonts.find(font => normalizeFontFamily(font.family) == fontFamilyName && font.style == fontStyle)) {
  499. const fontWeight = getFontWeight(computedStyle.getPropertyValue("font-weight"));
  500. const fontVariant = computedStyle.getPropertyValue("font-variant") || "normal";
  501. const value = [fontFamilyName, fontWeight, fontStyle, fontVariant];
  502. usedFonts.set(JSON$1.stringify(value), [fontFamilyName, fontWeight, fontStyle, fontVariant]);
  503. }
  504. });
  505. }
  506. }
  507. function getShadowRoot(element) {
  508. const chrome = globalThis.chrome;
  509. if (element.openOrClosedShadowRoot) {
  510. return element.openOrClosedShadowRoot;
  511. } else if (chrome && chrome.dom && chrome.dom.openOrClosedShadowRoot) {
  512. try {
  513. return chrome.dom.openOrClosedShadowRoot(element);
  514. } catch (error) {
  515. return element.shadowRoot;
  516. }
  517. } else {
  518. return element.shadowRoot;
  519. }
  520. }
  521. function normalizeFontFamily(fontFamilyName = "") {
  522. return removeQuotes(process$1(fontFamilyName.trim())).toLowerCase();
  523. }
  524. function testHiddenElement(element, computedStyle) {
  525. let hidden = false;
  526. if (computedStyle) {
  527. const display = computedStyle.getPropertyValue("display");
  528. const opacity = computedStyle.getPropertyValue("opacity");
  529. const visibility = computedStyle.getPropertyValue("visibility");
  530. hidden = display == "none";
  531. if (!hidden && (opacity == "0" || visibility == "hidden") && element.getBoundingClientRect) {
  532. const boundingRect = element.getBoundingClientRect();
  533. hidden = !boundingRect.width && !boundingRect.height;
  534. }
  535. }
  536. return Boolean(hidden);
  537. }
  538. function postProcessDoc(doc, markedElements, invalidElements) {
  539. doc.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
  540. element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  541. element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
  542. if (doc.body.firstChild) {
  543. doc.body.insertBefore(element, doc.body.firstChild);
  544. } else {
  545. doc.body.appendChild(element);
  546. }
  547. });
  548. doc.querySelectorAll("meta[disabled-http-equiv]").forEach(element => {
  549. element.setAttribute("http-equiv", element.getAttribute("disabled-http-equiv"));
  550. element.removeAttribute("disabled-http-equiv");
  551. });
  552. if (doc.head) {
  553. doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
  554. }
  555. if (!markedElements) {
  556. const singleFileAttributes = [REMOVED_CONTENT_ATTRIBUTE_NAME, HIDDEN_FRAME_ATTRIBUTE_NAME, HIDDEN_CONTENT_ATTRIBUTE_NAME, PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, IMAGE_ATTRIBUTE_NAME, POSTER_ATTRIBUTE_NAME, VIDEO_ATTRIBUTE_NAME, CANVAS_ATTRIBUTE_NAME, INPUT_VALUE_ATTRIBUTE_NAME, SHADOW_ROOT_ATTRIBUTE_NAME, STYLESHEET_ATTRIBUTE_NAME, ASYNC_SCRIPT_ATTRIBUTE_NAME];
  557. markedElements = doc.querySelectorAll(singleFileAttributes.map(name => "[" + name + "]").join(","));
  558. }
  559. markedElements.forEach(element => {
  560. element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME);
  561. element.removeAttribute(HIDDEN_CONTENT_ATTRIBUTE_NAME);
  562. element.removeAttribute(KEPT_CONTENT_ATTRIBUTE_NAME);
  563. element.removeAttribute(HIDDEN_FRAME_ATTRIBUTE_NAME);
  564. element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
  565. element.removeAttribute(IMAGE_ATTRIBUTE_NAME);
  566. element.removeAttribute(POSTER_ATTRIBUTE_NAME);
  567. element.removeAttribute(VIDEO_ATTRIBUTE_NAME);
  568. element.removeAttribute(CANVAS_ATTRIBUTE_NAME);
  569. element.removeAttribute(INPUT_VALUE_ATTRIBUTE_NAME);
  570. element.removeAttribute(SHADOW_ROOT_ATTRIBUTE_NAME);
  571. element.removeAttribute(STYLESHEET_ATTRIBUTE_NAME);
  572. element.removeAttribute(ASYNC_SCRIPT_ATTRIBUTE_NAME);
  573. element.removeAttribute(STYLE_ATTRIBUTE_NAME);
  574. });
  575. if (invalidElements) {
  576. invalidElements.forEach((placeholderElement, element) => placeholderElement.replaceWith(element));
  577. }
  578. }
  579. function getStylesheetsData(doc) {
  580. if (doc) {
  581. const contents = [];
  582. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  583. try {
  584. const tempStyleElement = doc.createElement("style");
  585. tempStyleElement.textContent = styleElement.textContent;
  586. doc.body.appendChild(tempStyleElement);
  587. const stylesheet = tempStyleElement.sheet;
  588. tempStyleElement.remove();
  589. if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
  590. styleElement.setAttribute(STYLESHEET_ATTRIBUTE_NAME, styleIndex);
  591. contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(cssRule => cssRule.cssText).join("\n");
  592. }
  593. } catch (error) {
  594. // ignored
  595. }
  596. });
  597. return contents;
  598. }
  599. }
  600. function getSize(win, imageElement, computedStyle) {
  601. let pxWidth = imageElement.naturalWidth;
  602. let pxHeight = imageElement.naturalHeight;
  603. if (!pxWidth && !pxHeight) {
  604. const noStyleAttribute = imageElement.getAttribute("style") == null;
  605. computedStyle = computedStyle || getComputedStyle(win, imageElement);
  606. if (computedStyle) {
  607. let removeBorderWidth = false;
  608. if (computedStyle.getPropertyValue("box-sizing") == "content-box") {
  609. const boxSizingValue = imageElement.style.getPropertyValue("box-sizing");
  610. const boxSizingPriority = imageElement.style.getPropertyPriority("box-sizing");
  611. const clientWidth = imageElement.clientWidth;
  612. imageElement.style.setProperty("box-sizing", "border-box", "important");
  613. removeBorderWidth = imageElement.clientWidth != clientWidth;
  614. if (boxSizingValue) {
  615. imageElement.style.setProperty("box-sizing", boxSizingValue, boxSizingPriority);
  616. } else {
  617. imageElement.style.removeProperty("box-sizing");
  618. }
  619. }
  620. let paddingLeft, paddingRight, paddingTop, paddingBottom, borderLeft, borderRight, borderTop, borderBottom;
  621. paddingLeft = getWidth("padding-left", computedStyle);
  622. paddingRight = getWidth("padding-right", computedStyle);
  623. paddingTop = getWidth("padding-top", computedStyle);
  624. paddingBottom = getWidth("padding-bottom", computedStyle);
  625. if (removeBorderWidth) {
  626. borderLeft = getWidth("border-left-width", computedStyle);
  627. borderRight = getWidth("border-right-width", computedStyle);
  628. borderTop = getWidth("border-top-width", computedStyle);
  629. borderBottom = getWidth("border-bottom-width", computedStyle);
  630. } else {
  631. borderLeft = borderRight = borderTop = borderBottom = 0;
  632. }
  633. pxWidth = Math.max(0, imageElement.clientWidth - paddingLeft - paddingRight - borderLeft - borderRight);
  634. pxHeight = Math.max(0, imageElement.clientHeight - paddingTop - paddingBottom - borderTop - borderBottom);
  635. if (noStyleAttribute) {
  636. imageElement.removeAttribute("style");
  637. }
  638. }
  639. }
  640. return { pxWidth, pxHeight };
  641. }
  642. function getWidth(styleName, computedStyle) {
  643. if (computedStyle.getPropertyValue(styleName).endsWith("px")) {
  644. return parseFloat(computedStyle.getPropertyValue(styleName));
  645. }
  646. }
  647. function getFontsData() {
  648. return getFontsData$1();
  649. }
  650. function serialize(doc) {
  651. const docType = doc.doctype;
  652. let docTypeString = "";
  653. if (docType) {
  654. docTypeString = "<!DOCTYPE " + docType.nodeName;
  655. if (docType.publicId) {
  656. docTypeString += " PUBLIC \"" + docType.publicId + "\"";
  657. if (docType.systemId) {
  658. docTypeString += " \"" + docType.systemId + "\"";
  659. }
  660. } else if (docType.systemId) {
  661. docTypeString += " SYSTEM \"" + docType.systemId + "\"";
  662. } if (docType.internalSubset) {
  663. docTypeString += " [" + docType.internalSubset + "]";
  664. }
  665. docTypeString += "> ";
  666. }
  667. return docTypeString + doc.documentElement.outerHTML;
  668. }
  669. function removeQuotes(string) {
  670. if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
  671. string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
  672. } else {
  673. string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
  674. }
  675. return string.trim();
  676. }
  677. function getFontWeight(weight) {
  678. return FONT_WEIGHTS[weight.toLowerCase().trim()] || weight;
  679. }
  680. function getComputedStyle(win, element, pseudoElement) {
  681. try {
  682. return win.getComputedStyle(element, pseudoElement);
  683. } catch (error) {
  684. // ignored
  685. }
  686. }
  687. /*
  688. * Copyright 2010-2022 Gildas Lormeau
  689. * contact : gildas.lormeau <at> gmail.com
  690. *
  691. * This file is part of SingleFile.
  692. *
  693. * The code in this file is free software: you can redistribute it and/or
  694. * modify it under the terms of the GNU Affero General Public License
  695. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  696. * of the License, or (at your option) any later version.
  697. *
  698. * The code in this file is distributed in the hope that it will be useful,
  699. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  700. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  701. * General Public License for more details.
  702. *
  703. * As additional permission under GNU AGPL version 3 section 7, you may
  704. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  705. * AGPL normally required by section 4, provided you include this license
  706. * notice and a URL through which recipients can access the Corresponding
  707. * Source.
  708. */
  709. const helper$1 = {
  710. LAZY_SRC_ATTRIBUTE_NAME,
  711. SINGLE_FILE_UI_ELEMENT_CLASS
  712. };
  713. const MAX_IDLE_TIMEOUT_CALLS = 10;
  714. const ATTRIBUTES_MUTATION_TYPE = "attributes";
  715. const browser$1 = globalThis.browser;
  716. const document$1 = globalThis.document;
  717. const MutationObserver = globalThis.MutationObserver;
  718. const addEventListener$1 = (type, listener, options) => globalThis.addEventListener(type, listener, options);
  719. const removeEventListener = (type, listener, options) => globalThis.removeEventListener(type, listener, options);
  720. const timeouts = new Map();
  721. let idleTimeoutCalls;
  722. if (browser$1 && browser$1.runtime && browser$1.runtime.onMessage && browser$1.runtime.onMessage.addListener) {
  723. browser$1.runtime.onMessage.addListener(message => {
  724. if (message.method == "singlefile.lazyTimeout.onTimeout") {
  725. const timeoutData = timeouts.get(message.type);
  726. if (timeoutData) {
  727. timeouts.delete(message.type);
  728. try {
  729. timeoutData.callback();
  730. } catch (error) {
  731. clearRegularTimeout(message.type);
  732. }
  733. }
  734. }
  735. });
  736. }
  737. async function process(options) {
  738. if (document$1.documentElement) {
  739. timeouts.clear();
  740. const bodyHeight = document$1.body ? Math.max(document$1.body.scrollHeight, document$1.documentElement.scrollHeight) : document$1.documentElement.scrollHeight;
  741. const bodyWidth = document$1.body ? Math.max(document$1.body.scrollWidth, document$1.documentElement.scrollWidth) : document$1.documentElement.scrollWidth;
  742. if (bodyHeight > globalThis.innerHeight || bodyWidth > globalThis.innerWidth) {
  743. const maxScrollY = Math.max(bodyHeight - (globalThis.innerHeight * 1.5), 0);
  744. const maxScrollX = Math.max(bodyWidth - (globalThis.innerWidth * 1.5), 0);
  745. if (globalThis.scrollY < maxScrollY || globalThis.scrollX < maxScrollX) {
  746. return triggerLazyLoading(options);
  747. }
  748. }
  749. }
  750. }
  751. function triggerLazyLoading(options) {
  752. idleTimeoutCalls = 0;
  753. return new Promise(async resolve => { // eslint-disable-line no-async-promise-executor
  754. let loadingImages;
  755. const pendingImages = new Set();
  756. const observer = new MutationObserver(async mutations => {
  757. mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
  758. if (mutations.length) {
  759. const updated = mutations.filter(mutation => {
  760. if (mutation.attributeName == "src") {
  761. mutation.target.setAttribute(helper$1.LAZY_SRC_ATTRIBUTE_NAME, mutation.target.src);
  762. mutation.target.addEventListener("load", onResourceLoad);
  763. }
  764. if (mutation.attributeName == "src" || mutation.attributeName == "srcset" ||
  765. (mutation.target.tagName && mutation.target.tagName.toUpperCase() == "SOURCE")) {
  766. return !mutation.target.classList || !mutation.target.classList.contains(helper$1.SINGLE_FILE_UI_ELEMENT_CLASS);
  767. }
  768. });
  769. if (updated.length) {
  770. loadingImages = true;
  771. await deferForceLazyLoadEnd(observer, options, cleanupAndResolve);
  772. if (!pendingImages.size) {
  773. await deferLazyLoadEnd(observer, options, cleanupAndResolve);
  774. }
  775. }
  776. }
  777. });
  778. await setIdleTimeout(options.loadDeferredImagesMaxIdleTime * 2);
  779. await deferForceLazyLoadEnd(observer, options, cleanupAndResolve);
  780. observer.observe(document$1, { subtree: true, childList: true, attributes: true });
  781. addEventListener$1(LOAD_IMAGE_EVENT, onImageLoadEvent);
  782. addEventListener$1(IMAGE_LOADED_EVENT, onImageLoadedEvent);
  783. loadDeferredImagesStart(options);
  784. async function setIdleTimeout(delay) {
  785. await setAsyncTimeout("idleTimeout", async () => {
  786. if (!loadingImages) {
  787. clearAsyncTimeout("loadTimeout");
  788. clearAsyncTimeout("maxTimeout");
  789. lazyLoadEnd(observer, options, cleanupAndResolve);
  790. } else if (idleTimeoutCalls < MAX_IDLE_TIMEOUT_CALLS) {
  791. idleTimeoutCalls++;
  792. clearAsyncTimeout("idleTimeout");
  793. await setIdleTimeout(Math.max(500, delay / 2));
  794. }
  795. }, delay, options.loadDeferredImagesNativeTimeout);
  796. }
  797. function onResourceLoad(event) {
  798. const element = event.target;
  799. element.removeAttribute(helper$1.LAZY_SRC_ATTRIBUTE_NAME);
  800. element.removeEventListener("load", onResourceLoad);
  801. }
  802. async function onImageLoadEvent(event) {
  803. loadingImages = true;
  804. await deferForceLazyLoadEnd(observer, options, cleanupAndResolve);
  805. await deferLazyLoadEnd(observer, options, cleanupAndResolve);
  806. if (event.detail) {
  807. pendingImages.add(event.detail);
  808. }
  809. }
  810. async function onImageLoadedEvent(event) {
  811. await deferForceLazyLoadEnd(observer, options, cleanupAndResolve);
  812. await deferLazyLoadEnd(observer, options, cleanupAndResolve);
  813. pendingImages.delete(event.detail);
  814. if (!pendingImages.size) {
  815. await deferLazyLoadEnd(observer, options, cleanupAndResolve);
  816. }
  817. }
  818. function cleanupAndResolve(value) {
  819. observer.disconnect();
  820. removeEventListener(LOAD_IMAGE_EVENT, onImageLoadEvent);
  821. removeEventListener(IMAGE_LOADED_EVENT, onImageLoadedEvent);
  822. resolve(value);
  823. }
  824. });
  825. }
  826. async function deferLazyLoadEnd(observer, options, resolve) {
  827. await setAsyncTimeout("loadTimeout", () => lazyLoadEnd(observer, options, resolve), options.loadDeferredImagesMaxIdleTime, options.loadDeferredImagesNativeTimeout);
  828. }
  829. async function deferForceLazyLoadEnd(observer, options, resolve) {
  830. await setAsyncTimeout("maxTimeout", async () => {
  831. await clearAsyncTimeout("loadTimeout");
  832. await lazyLoadEnd(observer, options, resolve);
  833. }, options.loadDeferredImagesMaxIdleTime * 10, options.loadDeferredImagesNativeTimeout);
  834. }
  835. async function lazyLoadEnd(observer, options, resolve) {
  836. await clearAsyncTimeout("idleTimeout");
  837. loadDeferredImagesEnd(options);
  838. await setAsyncTimeout("endTimeout", async () => {
  839. await clearAsyncTimeout("maxTimeout");
  840. resolve();
  841. }, options.loadDeferredImagesMaxIdleTime / 2, options.loadDeferredImagesNativeTimeout);
  842. observer.disconnect();
  843. }
  844. async function setAsyncTimeout(type, callback, delay, forceNativeTimeout) {
  845. if (browser$1 && browser$1.runtime && browser$1.runtime.sendMessage && !forceNativeTimeout) {
  846. if (!timeouts.get(type) || !timeouts.get(type).pending) {
  847. const timeoutData = { callback, pending: true };
  848. timeouts.set(type, timeoutData);
  849. try {
  850. await browser$1.runtime.sendMessage({ method: "singlefile.lazyTimeout.setTimeout", type, delay });
  851. } catch (error) {
  852. setRegularTimeout(type, callback, delay);
  853. }
  854. timeoutData.pending = false;
  855. }
  856. } else {
  857. setRegularTimeout(type, callback, delay);
  858. }
  859. }
  860. function setRegularTimeout(type, callback, delay) {
  861. const timeoutId = timeouts.get(type);
  862. if (timeoutId) {
  863. globalThis.clearTimeout(timeoutId);
  864. }
  865. timeouts.set(type, callback);
  866. globalThis.setTimeout(callback, delay);
  867. }
  868. async function clearAsyncTimeout(type) {
  869. if (browser$1 && browser$1.runtime && browser$1.runtime.sendMessage) {
  870. try {
  871. await browser$1.runtime.sendMessage({ method: "singlefile.lazyTimeout.clearTimeout", type });
  872. } catch (error) {
  873. clearRegularTimeout(type);
  874. }
  875. } else {
  876. clearRegularTimeout(type);
  877. }
  878. }
  879. function clearRegularTimeout(type) {
  880. const previousTimeoutId = timeouts.get(type);
  881. timeouts.delete(type);
  882. if (previousTimeoutId) {
  883. globalThis.clearTimeout(previousTimeoutId);
  884. }
  885. }
  886. /*
  887. * Copyright 2010-2022 Gildas Lormeau
  888. * contact : gildas.lormeau <at> gmail.com
  889. *
  890. * This file is part of SingleFile.
  891. *
  892. * The code in this file is free software: you can redistribute it and/or
  893. * modify it under the terms of the GNU Affero General Public License
  894. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  895. * of the License, or (at your option) any later version.
  896. *
  897. * The code in this file is distributed in the hope that it will be useful,
  898. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  899. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  900. * General Public License for more details.
  901. *
  902. * As additional permission under GNU AGPL version 3 section 7, you may
  903. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  904. * AGPL normally required by section 4, provided you include this license
  905. * notice and a URL through which recipients can access the Corresponding
  906. * Source.
  907. */
  908. const helper = {
  909. ON_BEFORE_CAPTURE_EVENT_NAME,
  910. ON_AFTER_CAPTURE_EVENT_NAME,
  911. WIN_ID_ATTRIBUTE_NAME,
  912. WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
  913. preProcessDoc,
  914. serialize,
  915. postProcessDoc,
  916. getShadowRoot
  917. };
  918. const FRAMES_CSS_SELECTOR = "iframe, frame, object[type=\"text/html\"][data]";
  919. const ALL_ELEMENTS_CSS_SELECTOR = "*";
  920. const INIT_REQUEST_MESSAGE = "singlefile.frameTree.initRequest";
  921. const ACK_INIT_REQUEST_MESSAGE = "singlefile.frameTree.ackInitRequest";
  922. const CLEANUP_REQUEST_MESSAGE = "singlefile.frameTree.cleanupRequest";
  923. const INIT_RESPONSE_MESSAGE = "singlefile.frameTree.initResponse";
  924. const TARGET_ORIGIN = "*";
  925. const TIMEOUT_INIT_REQUEST_MESSAGE = 5000;
  926. const TIMEOUT_INIT_RESPONSE_MESSAGE = 10000;
  927. const TOP_WINDOW_ID = "0";
  928. const WINDOW_ID_SEPARATOR = ".";
  929. const TOP_WINDOW = globalThis.window == globalThis.top;
  930. const browser = globalThis.browser;
  931. const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
  932. const top = globalThis.top;
  933. const MessageChannel = globalThis.MessageChannel;
  934. const document = globalThis.document;
  935. const JSON = globalThis.JSON;
  936. let sessions = globalThis.sessions;
  937. if (!sessions) {
  938. sessions = globalThis.sessions = new Map();
  939. }
  940. let windowId;
  941. if (TOP_WINDOW) {
  942. windowId = TOP_WINDOW_ID;
  943. if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
  944. browser.runtime.onMessage.addListener(message => {
  945. if (message.method == INIT_RESPONSE_MESSAGE) {
  946. initResponse(message);
  947. return Promise.resolve({});
  948. } else if (message.method == ACK_INIT_REQUEST_MESSAGE) {
  949. clearFrameTimeout("requestTimeouts", message.sessionId, message.windowId);
  950. createFrameResponseTimeout(message.sessionId, message.windowId);
  951. return Promise.resolve({});
  952. }
  953. });
  954. }
  955. }
  956. addEventListener("message", async event => {
  957. if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
  958. event.preventDefault();
  959. event.stopPropagation();
  960. const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length));
  961. if (message.method == INIT_REQUEST_MESSAGE) {
  962. if (event.source) {
  963. sendMessage(event.source, { method: ACK_INIT_REQUEST_MESSAGE, windowId: message.windowId, sessionId: message.sessionId });
  964. }
  965. if (!TOP_WINDOW) {
  966. globalThis.stop();
  967. if (message.options.loadDeferredImages) {
  968. process(message.options);
  969. }
  970. await initRequestAsync(message);
  971. }
  972. } else if (message.method == ACK_INIT_REQUEST_MESSAGE) {
  973. clearFrameTimeout("requestTimeouts", message.sessionId, message.windowId);
  974. createFrameResponseTimeout(message.sessionId, message.windowId);
  975. } else if (message.method == CLEANUP_REQUEST_MESSAGE) {
  976. cleanupRequest(message);
  977. } else if (message.method == INIT_RESPONSE_MESSAGE && sessions.get(message.sessionId)) {
  978. const port = event.ports[0];
  979. port.onmessage = event => initResponse(event.data);
  980. }
  981. }
  982. }, true);
  983. function getAsync(options) {
  984. const sessionId = getNewSessionId();
  985. options = JSON.parse(JSON.stringify(options));
  986. return new Promise(resolve => {
  987. sessions.set(sessionId, {
  988. frames: [],
  989. requestTimeouts: {},
  990. responseTimeouts: {},
  991. resolve: frames => {
  992. frames.sessionId = sessionId;
  993. resolve(frames);
  994. }
  995. });
  996. initRequestAsync({ windowId, sessionId, options });
  997. });
  998. }
  999. function getSync(options) {
  1000. const sessionId = getNewSessionId();
  1001. options = JSON.parse(JSON.stringify(options));
  1002. sessions.set(sessionId, {
  1003. frames: [],
  1004. requestTimeouts: {},
  1005. responseTimeouts: {}
  1006. });
  1007. initRequestSync({ windowId, sessionId, options });
  1008. const frames = sessions.get(sessionId).frames;
  1009. frames.sessionId = sessionId;
  1010. return frames;
  1011. }
  1012. function cleanup(sessionId) {
  1013. sessions.delete(sessionId);
  1014. cleanupRequest({ windowId, sessionId, options: { sessionId } });
  1015. }
  1016. function getNewSessionId() {
  1017. return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("");
  1018. }
  1019. function initRequestSync(message) {
  1020. const sessionId = message.sessionId;
  1021. const waitForUserScript = globalThis[helper.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];
  1022. delete globalThis._singleFile_cleaningUp;
  1023. if (!TOP_WINDOW) {
  1024. windowId = globalThis.frameId = message.windowId;
  1025. }
  1026. processFrames(document, message.options, windowId, sessionId);
  1027. if (!TOP_WINDOW) {
  1028. if (message.options.userScriptEnabled && waitForUserScript) {
  1029. waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
  1030. }
  1031. sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options, message.scrolling)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
  1032. if (message.options.userScriptEnabled && waitForUserScript) {
  1033. waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
  1034. }
  1035. delete document.documentElement.dataset.requestedFrameId;
  1036. }
  1037. }
  1038. async function initRequestAsync(message) {
  1039. const sessionId = message.sessionId;
  1040. const waitForUserScript = globalThis[helper.WAIT_FOR_USERSCRIPT_PROPERTY_NAME];
  1041. delete globalThis._singleFile_cleaningUp;
  1042. if (!TOP_WINDOW) {
  1043. windowId = globalThis.frameId = message.windowId;
  1044. }
  1045. processFrames(document, message.options, windowId, sessionId);
  1046. if (!TOP_WINDOW) {
  1047. if (message.options.userScriptEnabled && waitForUserScript) {
  1048. await waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
  1049. }
  1050. sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options, message.scrolling)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
  1051. if (message.options.userScriptEnabled && waitForUserScript) {
  1052. await waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
  1053. }
  1054. delete document.documentElement.dataset.requestedFrameId;
  1055. }
  1056. }
  1057. function cleanupRequest(message) {
  1058. if (!globalThis._singleFile_cleaningUp) {
  1059. globalThis._singleFile_cleaningUp = true;
  1060. const sessionId = message.sessionId;
  1061. cleanupFrames(getFrames(document), message.windowId, sessionId);
  1062. }
  1063. }
  1064. function initResponse(message) {
  1065. message.frames.forEach(frameData => clearFrameTimeout("responseTimeouts", message.sessionId, frameData.windowId));
  1066. const windowData = sessions.get(message.sessionId);
  1067. if (windowData) {
  1068. if (message.requestedFrameId) {
  1069. windowData.requestedFrameId = message.requestedFrameId;
  1070. }
  1071. message.frames.forEach(messageFrameData => {
  1072. let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
  1073. if (!frameData) {
  1074. frameData = { windowId: messageFrameData.windowId };
  1075. windowData.frames.push(frameData);
  1076. }
  1077. if (!frameData.processed) {
  1078. frameData.content = messageFrameData.content;
  1079. frameData.baseURI = messageFrameData.baseURI;
  1080. frameData.title = messageFrameData.title;
  1081. frameData.url = messageFrameData.url;
  1082. frameData.canvases = messageFrameData.canvases;
  1083. frameData.fonts = messageFrameData.fonts;
  1084. frameData.stylesheets = messageFrameData.stylesheets;
  1085. frameData.images = messageFrameData.images;
  1086. frameData.posters = messageFrameData.posters;
  1087. frameData.videos = messageFrameData.videos;
  1088. frameData.usedFonts = messageFrameData.usedFonts;
  1089. frameData.shadowRoots = messageFrameData.shadowRoots;
  1090. frameData.processed = messageFrameData.processed;
  1091. frameData.scrollPosition = messageFrameData.scrollPosition;
  1092. frameData.scrolling = messageFrameData.scrolling;
  1093. frameData.adoptedStyleSheets = messageFrameData.adoptedStyleSheets;
  1094. }
  1095. });
  1096. const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
  1097. if (!remainingFrames) {
  1098. windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(WINDOW_ID_SEPARATOR).length - frame1.windowId.split(WINDOW_ID_SEPARATOR).length);
  1099. if (windowData.resolve) {
  1100. if (windowData.requestedFrameId) {
  1101. windowData.frames.forEach(frameData => {
  1102. if (frameData.windowId == windowData.requestedFrameId) {
  1103. frameData.requestedFrame = true;
  1104. }
  1105. });
  1106. }
  1107. windowData.resolve(windowData.frames);
  1108. }
  1109. }
  1110. }
  1111. }
  1112. function processFrames(doc, options, parentWindowId, sessionId) {
  1113. const frameElements = getFrames(doc);
  1114. processFramesAsync(doc, frameElements, options, parentWindowId, sessionId);
  1115. if (frameElements.length) {
  1116. processFramesSync(doc, frameElements, options, parentWindowId, sessionId);
  1117. }
  1118. }
  1119. function processFramesAsync(doc, frameElements, options, parentWindowId, sessionId) {
  1120. const frames = [];
  1121. let requestTimeouts;
  1122. if (sessions.get(sessionId)) {
  1123. requestTimeouts = sessions.get(sessionId).requestTimeouts;
  1124. } else {
  1125. requestTimeouts = {};
  1126. sessions.set(sessionId, { requestTimeouts });
  1127. }
  1128. frameElements.forEach((frameElement, frameIndex) => {
  1129. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  1130. frameElement.setAttribute(helper.WIN_ID_ATTRIBUTE_NAME, windowId);
  1131. frames.push({ windowId });
  1132. });
  1133. sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
  1134. frameElements.forEach((frameElement, frameIndex) => {
  1135. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  1136. try {
  1137. sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options, scrolling: frameElement.scrolling });
  1138. } catch (error) {
  1139. // ignored
  1140. }
  1141. requestTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
  1142. });
  1143. delete doc.documentElement.dataset.requestedFrameId;
  1144. }
  1145. function processFramesSync(doc, frameElements, options, parentWindowId, sessionId) {
  1146. const frames = [];
  1147. frameElements.forEach((frameElement, frameIndex) => {
  1148. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  1149. let frameDoc;
  1150. try {
  1151. frameDoc = frameElement.contentDocument;
  1152. } catch (error) {
  1153. // ignored
  1154. }
  1155. if (frameDoc) {
  1156. try {
  1157. const frameWindow = frameElement.contentWindow;
  1158. frameWindow.stop();
  1159. clearFrameTimeout("requestTimeouts", sessionId, windowId);
  1160. processFrames(frameDoc, options, windowId, sessionId);
  1161. frames.push(getFrameData(frameDoc, frameWindow, windowId, options, frameElement.scrolling));
  1162. } catch (error) {
  1163. frames.push({ windowId, processed: true });
  1164. }
  1165. }
  1166. });
  1167. sendInitResponse({ frames, sessionId, requestedFrameId: doc.documentElement.dataset.requestedFrameId && parentWindowId });
  1168. delete doc.documentElement.dataset.requestedFrameId;
  1169. }
  1170. function clearFrameTimeout(type, sessionId, windowId) {
  1171. const session = sessions.get(sessionId);
  1172. if (session && session[type]) {
  1173. const timeout = session[type][windowId];
  1174. if (timeout) {
  1175. globalThis.clearTimeout(timeout);
  1176. delete session[type][windowId];
  1177. }
  1178. }
  1179. }
  1180. function createFrameResponseTimeout(sessionId, windowId) {
  1181. const session = sessions.get(sessionId);
  1182. if (session && session.responseTimeouts) {
  1183. session.responseTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId: windowId, processed: true }], sessionId: sessionId }), TIMEOUT_INIT_RESPONSE_MESSAGE);
  1184. }
  1185. }
  1186. function cleanupFrames(frameElements, parentWindowId, sessionId) {
  1187. frameElements.forEach((frameElement, frameIndex) => {
  1188. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  1189. frameElement.removeAttribute(helper.WIN_ID_ATTRIBUTE_NAME);
  1190. try {
  1191. sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId });
  1192. } catch (error) {
  1193. // ignored
  1194. }
  1195. });
  1196. frameElements.forEach((frameElement, frameIndex) => {
  1197. const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
  1198. let frameDoc;
  1199. try {
  1200. frameDoc = frameElement.contentDocument;
  1201. } catch (error) {
  1202. // ignored
  1203. }
  1204. if (frameDoc) {
  1205. try {
  1206. cleanupFrames(getFrames(frameDoc), windowId, sessionId);
  1207. } catch (error) {
  1208. // ignored
  1209. }
  1210. }
  1211. });
  1212. }
  1213. function sendInitResponse(message) {
  1214. message.method = INIT_RESPONSE_MESSAGE;
  1215. try {
  1216. top.singlefile.processors.frameTree.initResponse(message);
  1217. } catch (error) {
  1218. sendMessage(top, message, true);
  1219. }
  1220. }
  1221. function sendMessage(targetWindow, message, useChannel) {
  1222. if (targetWindow == top && browser && browser.runtime && browser.runtime.sendMessage) {
  1223. browser.runtime.sendMessage(message);
  1224. } else {
  1225. if (useChannel) {
  1226. const channel = new MessageChannel();
  1227. targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method, sessionId: message.sessionId }), TARGET_ORIGIN, [channel.port2]);
  1228. channel.port1.postMessage(message);
  1229. } else {
  1230. targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
  1231. }
  1232. }
  1233. }
  1234. function getFrameData(document, globalThis, windowId, options, scrolling) {
  1235. const docData = helper.preProcessDoc(document, globalThis, options);
  1236. const content = helper.serialize(document);
  1237. helper.postProcessDoc(document, docData.markedElements, docData.invalidElements);
  1238. const baseURI = document.baseURI.split("#")[0];
  1239. return {
  1240. windowId,
  1241. content,
  1242. baseURI,
  1243. url: document.location.href,
  1244. title: document.title,
  1245. canvases: docData.canvases,
  1246. fonts: docData.fonts,
  1247. stylesheets: docData.stylesheets,
  1248. images: docData.images,
  1249. posters: docData.posters,
  1250. videos: docData.videos,
  1251. usedFonts: docData.usedFonts,
  1252. shadowRoots: docData.shadowRoots,
  1253. scrollPosition: docData.scrollPosition,
  1254. scrolling,
  1255. adoptedStyleSheets: docData.adoptedStyleSheets,
  1256. processed: true
  1257. };
  1258. }
  1259. function getFrames(document) {
  1260. let frames = Array.from(document.querySelectorAll(FRAMES_CSS_SELECTOR));
  1261. document.querySelectorAll(ALL_ELEMENTS_CSS_SELECTOR).forEach(element => {
  1262. const shadowRoot = helper.getShadowRoot(element);
  1263. if (shadowRoot) {
  1264. frames = frames.concat(...shadowRoot.querySelectorAll(FRAMES_CSS_SELECTOR));
  1265. }
  1266. });
  1267. return frames;
  1268. }
  1269. exports.TIMEOUT_INIT_REQUEST_MESSAGE = TIMEOUT_INIT_REQUEST_MESSAGE;
  1270. exports.cleanup = cleanup;
  1271. exports.getAsync = getAsync;
  1272. exports.getSync = getSync;
  1273. exports.initResponse = initResponse;
  1274. Object.defineProperty(exports, '__esModule', { value: true });
  1275. }));