single-file-core.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. this.SingleFileCore = this.SingleFileCore || (() => {
  21. const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
  22. const SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME = "data-single-file-selected-content-root";
  23. let Download, DOM, URL, sessionId = 0;
  24. function getClass(...args) {
  25. [Download, DOM, URL] = args;
  26. return class {
  27. constructor(options) {
  28. this.options = options;
  29. options.sessionId = sessionId;
  30. sessionId++;
  31. this.SELECTED_CONTENT_ATTRIBUTE_NAME = SELECTED_CONTENT_ATTRIBUTE_NAME;
  32. this.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME = SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME;
  33. }
  34. async initialize() {
  35. this.processor = new PageProcessor(this.options);
  36. await this.processor.loadPage();
  37. await this.processor.initialize();
  38. }
  39. async preparePageData() {
  40. await this.processor.preparePageData();
  41. }
  42. getPageData() {
  43. return this.processor.getPageData();
  44. }
  45. };
  46. }
  47. // -------------
  48. // ProgressEvent
  49. // -------------
  50. const PAGE_LOADING = "page-loading";
  51. const PAGE_LOADED = "page-loaded";
  52. const RESOURCES_INITIALIZING = "resource-initializing";
  53. const RESOURCES_INITIALIZED = "resources-initialized";
  54. const RESOURCE_LOADED = "resource-loaded";
  55. const PAGE_ENDED = "page-ended";
  56. class ProgressEvent {
  57. constructor(type, details) {
  58. return { type, details, PAGE_LOADING, PAGE_LOADED, RESOURCES_INITIALIZING, RESOURCES_INITIALIZED, RESOURCE_LOADED, PAGE_ENDED };
  59. }
  60. }
  61. // -------------
  62. // PageProcessor
  63. // -------------
  64. const STAGES = [{
  65. sync: [
  66. { action: "removeUIElements" },
  67. { action: "replaceStyleContents" },
  68. { option: "removeSrcSet", action: "removeSrcSet" },
  69. { option: "removeFrames", action: "removeFrames" },
  70. { option: "removeImports", action: "removeImports" },
  71. { option: "removeScripts", action: "removeScripts" },
  72. { action: "removeDiscardedResources" },
  73. { action: "resetCharsetMeta" },
  74. { option: "compressHTML", action: "compressHTML" },
  75. { option: "insertFaviconLink", action: "insertFaviconLink" },
  76. { action: "resolveHrefs" },
  77. { action: "replaceCanvasElements" },
  78. { option: "removeHiddenElements", action: "removeHiddenElements" }
  79. ],
  80. async: [
  81. { action: "inlineStylesheets" },
  82. { action: "linkStylesheets" },
  83. { action: "attributeStyles" },
  84. { option: "!removeFrames", action: "frames" },
  85. { option: "!removeImports", action: "htmlImports" }
  86. ]
  87. }, {
  88. sync: [
  89. { option: "removeUnusedStyles", action: "removeUnusedStyles" },
  90. { option: "compressHTML", action: "compressHTML" },
  91. { option: "removeAlternativeFonts", action: "removeAlternativeFonts" },
  92. { option: "compressCSS", action: "compressCSS" },
  93. ],
  94. async: [
  95. { action: "inlineStylesheets" },
  96. { action: "attributeStyles" },
  97. { action: "pageResources" },
  98. { option: "!removeScripts", action: "scripts" }
  99. ]
  100. }, {
  101. sync: [
  102. { option: "lazyLoadImages", action: "lazyLoadImages" },
  103. { option: "removeAlternativeFonts", action: "postRemoveAlternativeFonts" }
  104. ],
  105. async: [
  106. { option: "!removeFrames", action: "frames" },
  107. { option: "!removeImports", action: "htmlImports" },
  108. ]
  109. }, {
  110. sync: [
  111. { option: "compressHTML", action: "postCompressHTML" },
  112. { option: "insertSingleFileComment", action: "insertSingleFileComment" },
  113. { action: "removeDefaultHeadTags" }
  114. ]
  115. }];
  116. class PageProcessor {
  117. constructor(options) {
  118. this.options = options;
  119. this.options.url = this.options.url || this.options.doc.location.href;
  120. this.processor = new DOMProcessor(options);
  121. if (this.options.doc) {
  122. const docData = DOM.preProcessDoc(this.options.doc, this.options.win, this.options);
  123. this.options.canvasData = docData.canvasData;
  124. this.options.stylesheetContents = docData.stylesheetContents;
  125. this.options.imageData = docData.imageData;
  126. }
  127. this.options.content = this.options.content || (this.options.doc ? DOM.serialize(this.options.doc, false) : null);
  128. this.onprogress = options.onprogress || (() => { });
  129. }
  130. async loadPage() {
  131. this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
  132. await this.processor.loadPage(this.options.content);
  133. this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
  134. }
  135. async initialize() {
  136. this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
  137. await this.executeStage(0);
  138. this.pendingPromises = this.executeStage(1);
  139. if (this.options.doc) {
  140. DOM.postProcessDoc(this.options.doc, this.options);
  141. this.options.doc = null;
  142. this.options.win = null;
  143. }
  144. this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() }));
  145. }
  146. async preparePageData() {
  147. if (!this.options.windowId) {
  148. await this.processor.retrieveResources(
  149. details => {
  150. details.pageURL = this.options.url;
  151. this.onprogress(new ProgressEvent(RESOURCE_LOADED, details));
  152. });
  153. }
  154. await this.pendingPromises;
  155. await this.executeStage(2);
  156. await this.executeStage(3);
  157. }
  158. getPageData() {
  159. this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
  160. return this.processor.getPageData();
  161. }
  162. async executeStage(step) {
  163. STAGES[step].sync.forEach(task => this.executeTask(task, !step));
  164. if (STAGES[step].async) {
  165. return Promise.all(STAGES[step].async.map(task => this.executeTask(task, !step)));
  166. }
  167. }
  168. executeTask(task, initialization) {
  169. if (!task.option || ((task.option.startsWith("!") && !this.options[task.option]) || this.options[task.option])) {
  170. return this.processor[task.action](initialization);
  171. }
  172. }
  173. }
  174. // --------
  175. // BatchRequest
  176. // --------
  177. class BatchRequest {
  178. constructor() {
  179. this.requests = new Map();
  180. }
  181. async addURL(resourceURL, asDataURI = true) {
  182. return new Promise((resolve, reject) => {
  183. const requestKey = JSON.stringify([resourceURL, asDataURI]);
  184. const resourceRequests = this.requests.get(requestKey);
  185. if (resourceRequests) {
  186. resourceRequests.push({ resolve, reject });
  187. } else {
  188. this.requests.set(requestKey, [{ resolve, reject }]);
  189. }
  190. });
  191. }
  192. getMaxResources() {
  193. return Array.from(this.requests.keys()).length;
  194. }
  195. async run(onloadListener, options) {
  196. const resourceURLs = Array.from(this.requests.keys());
  197. let indexResource = 0;
  198. return Promise.all(resourceURLs.map(async requestKey => {
  199. const [resourceURL, asDataURI] = JSON.parse(requestKey);
  200. const resourceRequests = this.requests.get(requestKey);
  201. try {
  202. const dataURI = await Download.getContent(resourceURL, { asDataURI, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
  203. indexResource = indexResource + 1;
  204. onloadListener({ index: indexResource, max: resourceURLs.length, url: resourceURL });
  205. resourceRequests.forEach(resourceRequest => resourceRequest.resolve(dataURI));
  206. } catch (error) {
  207. indexResource = indexResource + 1;
  208. onloadListener({ index: indexResource, max: resourceURLs.length, url: resourceURL });
  209. resourceRequests.forEach(resourceRequest => resourceRequest.reject(error));
  210. }
  211. this.requests.delete(requestKey);
  212. }));
  213. }
  214. }
  215. // ------------
  216. // DOMProcessor
  217. // ------------
  218. const ESCAPED_FRAGMENT = "_escaped_fragment_=";
  219. const EMPTY_DATA_URI = "data:base64,";
  220. const batchRequest = new BatchRequest();
  221. class DOMProcessor {
  222. constructor(options) {
  223. this.options = options;
  224. this.stats = new Stats(options);
  225. this.baseURI = DomUtil.normalizeURL(options.url);
  226. }
  227. async loadPage(pageContent) {
  228. if (!pageContent || this.options.saveRawPage) {
  229. pageContent = await Download.getContent(this.baseURI, { asDataURI: false, maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
  230. }
  231. this.doc = DOM.createDoc(pageContent, this.baseURI);
  232. if (!pageContent && this.doc.querySelector("meta[name=fragment][content=\"!\"]") && !this.baseURI.endsWith("?" + ESCAPED_FRAGMENT) && !this.baseURI.endsWith("&" + ESCAPED_FRAGMENT)) {
  233. await DOMProcessor.loadEscapedFragmentPage();
  234. }
  235. }
  236. async loadEscapedFragmentPage() {
  237. if (this.baseURI.includes("?")) {
  238. this.baseURI += "&";
  239. } else {
  240. this.baseURI += "?";
  241. }
  242. this.baseURI += ESCAPED_FRAGMENT;
  243. await this.loadPage();
  244. }
  245. async retrieveResources(onloadListener) {
  246. this.stats.set("processed", "resources", batchRequest.getMaxResources());
  247. await batchRequest.run(onloadListener, this.options);
  248. }
  249. getPageData() {
  250. DOM.postProcessDoc(this.doc, this.options);
  251. if (this.options.selected) {
  252. const rootElement = this.doc.querySelector("[" + SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME + "]");
  253. if (rootElement) {
  254. DomProcessorHelper.isolateElements(rootElement);
  255. rootElement.removeAttribute(SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME);
  256. rootElement.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME);
  257. }
  258. }
  259. const titleElement = this.doc.querySelector("title");
  260. let title;
  261. if (titleElement) {
  262. title = titleElement.textContent.trim();
  263. }
  264. const matchTitle = this.baseURI.match(/([^/]*)\/?(\.html?.*)$/) || this.baseURI.match(/\/\/([^/]*)\/?$/);
  265. const url = new URL(this.baseURI);
  266. let size;
  267. if (this.options.displayStats) {
  268. size = DOM.getContentSize(this.doc.documentElement.outerHTML);
  269. }
  270. const content = DOM.serialize(this.doc, this.options.compressHTML);
  271. if (this.options.displayStats) {
  272. const contentSize = DOM.getContentSize(content);
  273. this.stats.set("processed", "htmlBytes", contentSize);
  274. this.stats.add("discarded", "htmlBytes", size - contentSize);
  275. }
  276. return {
  277. stats: this.stats.data,
  278. title: title || (this.baseURI && matchTitle ? matchTitle[1] : (url.hostname ? url.hostname : "Untitled page")),
  279. content
  280. };
  281. }
  282. lazyLoadImages() {
  283. DOM.lazyLoad(this.doc);
  284. }
  285. removeDiscardedResources() {
  286. const objectElements = this.doc.querySelectorAll("applet, meta[http-equiv=refresh], object:not([type=\"image/svg+xml\"]):not([type=\"image/svg-xml\"]):not([type=\"text/html\"]), embed:not([src*=\".svg\"])");
  287. this.stats.set("discarded", "objects", objectElements.length);
  288. objectElements.forEach(element => element.remove());
  289. const replacedAttributeValue = this.doc.querySelectorAll("link[rel~=preconnect], link[rel~=prerender], link[rel~=dns-prefetch], link[rel~=preload], link[rel~=prefetch]");
  290. replacedAttributeValue.forEach(element => element.setAttribute("rel", element.getAttribute("rel").replace(/(preconnect|prerender|dns-prefetch|preload|prefetch)/g, "")));
  291. this.doc.querySelectorAll("[onload]").forEach(element => element.removeAttribute("onload"));
  292. this.doc.querySelectorAll("[onerror]").forEach(element => element.removeAttribute("onerror"));
  293. if (this.options.removeAudioSrc) {
  294. const audioSourceElements = this.doc.querySelectorAll("audio[src], audio > source[src]");
  295. this.stats.set("discarded", "audioSource", audioSourceElements.length);
  296. audioSourceElements.forEach(element => element.removeAttribute("src"));
  297. }
  298. if (this.options.removeVideoSrc) {
  299. const videoSourceElements = this.doc.querySelectorAll("video[src], video > source[src]");
  300. this.stats.set("discarded", "videoSource", videoSourceElements.length);
  301. videoSourceElements.forEach(element => element.removeAttribute("src"));
  302. }
  303. }
  304. removeDefaultHeadTags() {
  305. this.doc.querySelectorAll("base").forEach(element => element.remove());
  306. if (this.doc.head.querySelectorAll("*").length == 1 && this.doc.head.querySelector("meta[charset]") && this.doc.body.childNodes.length == 0) {
  307. this.doc.head.querySelector("meta[charset]").remove();
  308. }
  309. }
  310. removeUIElements() {
  311. this.doc.querySelectorAll("singlefile-infobar, singlefile-mask").forEach(element => element.remove());
  312. }
  313. removeScripts() {
  314. const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"])");
  315. this.stats.set("discarded", "scripts", scriptElements.length);
  316. scriptElements.forEach(element => element.remove());
  317. }
  318. removeFrames() {
  319. const frameElements = this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
  320. this.stats.set("discarded", "frames", frameElements.length);
  321. this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]").forEach(element => element.remove());
  322. }
  323. removeImports() {
  324. const importElements = this.doc.querySelectorAll("link[rel=import]");
  325. this.stats.set("discarded", "imports", importElements.length);
  326. importElements.forEach(element => element.remove());
  327. }
  328. resetCharsetMeta() {
  329. this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => element.remove());
  330. const metaElement = this.doc.createElement("meta");
  331. metaElement.setAttribute("charset", "utf-8");
  332. this.doc.head.insertBefore(metaElement, this.doc.head.firstElementChild);
  333. }
  334. insertFaviconLink() {
  335. let faviconElement = this.doc.querySelector("link[href][rel*=\"icon\"]");
  336. if (!faviconElement) {
  337. faviconElement = this.doc.createElement("link");
  338. faviconElement.setAttribute("type", "image/x-icon");
  339. faviconElement.setAttribute("rel", "shortcut icon");
  340. faviconElement.setAttribute("href", "/favicon.ico");
  341. }
  342. this.doc.head.appendChild(faviconElement);
  343. }
  344. resolveHrefs() {
  345. this.doc.querySelectorAll("[href]").forEach(element => {
  346. if (element.href) {
  347. const href = element.href.baseVal ? element.href.baseVal : element.href;
  348. const match = href.match(/(.*)#.*$/);
  349. if (!match || match[1] != this.baseURI) {
  350. element.setAttribute("href", href);
  351. }
  352. }
  353. });
  354. }
  355. removeUnusedStyles() {
  356. const stats = DOM.minifyCSS(this.doc);
  357. this.stats.set("processed", "cssRules", stats.processed);
  358. this.stats.set("discarded", "cssRules", stats.discarded);
  359. }
  360. removeAlternativeFonts() {
  361. DOM.minifyFonts(this.doc);
  362. }
  363. removeSrcSet() {
  364. this.doc.querySelectorAll("picture, img[srcset]").forEach(element => {
  365. const tagName = element.tagName.toLowerCase();
  366. const dataAttributeName = DOM.responsiveImagesAttributeName(this.options.sessionId);
  367. const imageData = this.options.imageData[Number(element.getAttribute(dataAttributeName))];
  368. element.removeAttribute(dataAttributeName);
  369. if (imageData) {
  370. if (tagName == "img") {
  371. if (imageData.source.src && imageData.source.naturalWidth > 1 && imageData.source.naturalHeight > 1) {
  372. element.removeAttribute("srcset");
  373. element.removeAttribute("sizes");
  374. element.src = imageData.source.src;
  375. }
  376. }
  377. if (tagName == "picture") {
  378. const imageElement = element.querySelector("img");
  379. if (imageData.source.src && imageData.source.naturalWidth > 1 && imageData.source.naturalHeight > 1) {
  380. imageElement.removeAttribute("srcset");
  381. imageElement.removeAttribute("sizes");
  382. imageElement.src = imageData.source.src;
  383. element.querySelectorAll("source").forEach(sourceElement => sourceElement.remove());
  384. } else {
  385. if (imageData.sources) {
  386. element.querySelectorAll("source").forEach(sourceElement => {
  387. if (!sourceElement.srcset && !sourceElement.dataset.srcset && !sourceElement.src) {
  388. sourceElement.remove();
  389. }
  390. });
  391. const sourceElements = element.querySelectorAll("source");
  392. if (sourceElements.length) {
  393. const lastSourceElement = sourceElements[sourceElements.length - 1];
  394. if (lastSourceElement.src) {
  395. imageElement.src = lastSourceElement.src;
  396. } else {
  397. imageElement.removeAttribute("src");
  398. }
  399. if (lastSourceElement.srcset || lastSourceElement.dataset.srcset) {
  400. imageElement.srcset = lastSourceElement.srcset || lastSourceElement.dataset.srcset;
  401. } else {
  402. imageElement.removeAttribute("srcset");
  403. }
  404. element.querySelectorAll("source").forEach(sourceElement => sourceElement.remove());
  405. }
  406. }
  407. }
  408. }
  409. }
  410. });
  411. }
  412. postRemoveAlternativeFonts() {
  413. DOM.minifyFonts(this.doc, true);
  414. if (this.options.compressCSS) {
  415. this.compressCSS();
  416. }
  417. }
  418. removeHiddenElements() {
  419. const hiddenElements = this.doc.querySelectorAll("[" + DOM.removedContentAttributeName(this.options.sessionId) + "]");
  420. this.stats.set("discarded", "hiddenElements", hiddenElements.length);
  421. hiddenElements.forEach(element => element.remove());
  422. }
  423. compressHTML() {
  424. let size;
  425. if (this.options.displayStats) {
  426. size = DOM.getContentSize(this.doc.documentElement.outerHTML);
  427. }
  428. DOM.minifyHTML(this.doc, { preservedSpaceAttributeName: DOM.preservedSpaceAttributeName(this.options.sessionId) });
  429. if (this.options.displayStats) {
  430. this.stats.add("discarded", "htmlBytes", size - DOM.getContentSize(this.doc.documentElement.outerHTML));
  431. }
  432. }
  433. postCompressHTML() {
  434. let size;
  435. if (this.options.displayStats) {
  436. size = DOM.getContentSize(this.doc.documentElement.outerHTML);
  437. }
  438. DOM.postMinifyHTML(this.doc);
  439. if (this.options.displayStats) {
  440. this.stats.add("discarded", "htmlBytes", size - DOM.getContentSize(this.doc.documentElement.outerHTML));
  441. }
  442. }
  443. compressCSS() {
  444. this.doc.querySelectorAll("style").forEach(styleElement => {
  445. if (styleElement) {
  446. styleElement.textContent = DOM.compressCSS(styleElement.textContent);
  447. }
  448. });
  449. this.doc.querySelectorAll("[style]").forEach(element => {
  450. element.setAttribute("style", DOM.compressCSS(element.getAttribute("style")));
  451. });
  452. }
  453. insertSingleFileComment() {
  454. const commentNode = this.doc.createComment("\n Archive processed by SingleFile \n url: " + this.baseURI + " \n saved date: " + new Date() + " \n");
  455. this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
  456. }
  457. replaceCanvasElements() {
  458. if (this.options.canvasData) {
  459. this.doc.querySelectorAll("canvas").forEach((canvasElement, indexCanvasElement) => {
  460. const canvasData = this.options.canvasData[indexCanvasElement];
  461. if (canvasData) {
  462. const imgElement = this.doc.createElement("img");
  463. imgElement.setAttribute("src", canvasData.dataURI);
  464. Array.from(canvasElement.attributes).forEach(attribute => {
  465. if (attribute.value) {
  466. imgElement.setAttribute(attribute.name, attribute.value);
  467. }
  468. });
  469. if (!imgElement.width && canvasData.width) {
  470. imgElement.style.pixelWidth = canvasData.width;
  471. }
  472. if (!imgElement.height && canvasData.height) {
  473. imgElement.style.pixelHeight = canvasData.height;
  474. }
  475. canvasElement.parentElement.replaceChild(imgElement, canvasElement);
  476. this.stats.add("processed", "canvas", 1);
  477. }
  478. });
  479. }
  480. }
  481. replaceStyleContents() {
  482. if (this.options.stylesheetContents) {
  483. this.doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  484. if (this.options.stylesheetContents[styleIndex]) {
  485. styleElement.textContent = this.options.stylesheetContents[styleIndex];
  486. }
  487. });
  488. }
  489. }
  490. async pageResources() {
  491. const resourcePromises = [
  492. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("link[href][rel*=\"icon\"]"), "href", this.baseURI),
  493. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("object[type=\"image/svg+xml\"], object[type=\"image/svg-xml\"]"), "data", this.baseURI),
  494. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("img[src], input[src][type=image], embed[src*=\".svg\"]"), "src", this.baseURI),
  495. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("video[poster]"), "poster", this.baseURI),
  496. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("*[background]"), "background", this.baseURI),
  497. DomProcessorHelper.processAttribute(this.doc.querySelectorAll("image"), "xlink:href", this.baseURI),
  498. DomProcessorHelper.processXLinks(this.doc.querySelectorAll("use"), this.baseURI),
  499. DomProcessorHelper.processSrcset(this.doc.querySelectorAll("[srcset]"), "srcset", this.baseURI)
  500. ];
  501. if (!this.options.removeAudioSrc) {
  502. resourcePromises.push(DomProcessorHelper.processAttribute(this.doc.querySelectorAll("audio[src], audio > source[src]"), "src", this.baseURI));
  503. }
  504. if (!this.options.removeVideoSrc) {
  505. resourcePromises.push(DomProcessorHelper.processAttribute(this.doc.querySelectorAll("video[src], video > source[src]"), "src", this.baseURI));
  506. }
  507. if (this.options.lazyLoadImages) {
  508. const imageSelectors = DOM.lazyLoaderImageSelectors();
  509. Object.keys(imageSelectors.src).forEach(selector => resourcePromises.push(DomProcessorHelper.processAttribute(this.doc.querySelectorAll(selector), imageSelectors.src[selector], this.baseURI)));
  510. Object.keys(imageSelectors.srcset).forEach(selector => resourcePromises.push(DomProcessorHelper.processSrcset(this.doc.querySelectorAll(selector), imageSelectors.srcset[selector], this.baseURI)));
  511. }
  512. await resourcePromises;
  513. }
  514. async inlineStylesheets(initialization) {
  515. await Promise.all(Array.from(this.doc.querySelectorAll("style")).map(async styleElement => {
  516. if (!initialization) {
  517. this.stats.add("processed", "styleSheets", 1);
  518. }
  519. let stylesheetContent = styleElement.textContent;
  520. if (initialization) {
  521. stylesheetContent = await DomProcessorHelper.resolveImportURLs(styleElement.textContent, this.baseURI, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
  522. } else {
  523. stylesheetContent = await DomProcessorHelper.processStylesheet(styleElement.textContent, this.baseURI);
  524. }
  525. styleElement.textContent = stylesheetContent;
  526. }));
  527. }
  528. async scripts() {
  529. await Promise.all(Array.from(this.doc.querySelectorAll("script[src]")).map(async scriptElement => {
  530. if (scriptElement.src) {
  531. this.stats.add("processed", "scripts", 1);
  532. const scriptContent = await Download.getContent(scriptElement.src, { asDataURI: false, maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
  533. scriptElement.textContent = scriptContent.replace(/<\/script>/gi, "<\\/script>");
  534. }
  535. scriptElement.removeAttribute("src");
  536. }));
  537. }
  538. async frames(initialization) {
  539. if (this.options.framesData) {
  540. const frameElements = Array.from(this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"));
  541. await Promise.all(frameElements.map(async frameElement => {
  542. DomProcessorHelper.setFrameEmptySrc(frameElement);
  543. frameElement.setAttribute("sandbox", "");
  544. const frameWindowId = frameElement.getAttribute(DOM.windowIdAttributeName(this.options.sessionId));
  545. if (frameWindowId) {
  546. const frameData = this.options.framesData.find(frame => frame.windowId == frameWindowId);
  547. if (frameData) {
  548. if (initialization) {
  549. const options = Object.create(this.options);
  550. options.insertSingleFileComment = false;
  551. options.insertFaviconLink = false;
  552. options.doc = null;
  553. options.win = null;
  554. options.url = frameData.baseURI;
  555. options.windowId = frameWindowId;
  556. if (frameData.content) {
  557. options.content = frameData.content;
  558. options.canvasData = frameData.canvasData;
  559. options.stylesheetContents = frameData.stylesheetContents;
  560. options.currentSrcImages = frameData.currentSrcImages;
  561. frameData.processor = new PageProcessor(options);
  562. frameData.frameElement = frameElement;
  563. await frameData.processor.loadPage();
  564. return frameData.processor.initialize();
  565. }
  566. } else {
  567. if (frameData.processor) {
  568. this.stats.add("processed", "frames", 1);
  569. await frameData.processor.preparePageData();
  570. const pageData = await frameData.processor.getPageData();
  571. frameElement.removeAttribute(DOM.windowIdAttributeName(this.options.sessionId));
  572. DomProcessorHelper.setFrameContent(frameElement, pageData.content);
  573. this.stats.addAll(pageData);
  574. } else {
  575. this.stats.add("discarded", "frames", 1);
  576. }
  577. }
  578. }
  579. }
  580. }));
  581. }
  582. }
  583. async htmlImports(initialization) {
  584. const linkElements = Array.from(this.doc.querySelectorAll("link[rel=import][href]"));
  585. if (!this.relImportProcessors) {
  586. this.relImportProcessors = new Map();
  587. }
  588. await Promise.all(linkElements.map(async linkElement => {
  589. if (initialization) {
  590. const resourceURL = linkElement.href;
  591. const options = Object.create(this.options);
  592. options.insertSingleFileComment = false;
  593. options.insertFaviconLink = false;
  594. options.doc = null;
  595. options.win = null;
  596. options.url = resourceURL;
  597. if (resourceURL) {
  598. if (resourceURL && resourceURL != this.baseURI && DomUtil.testValidPath(resourceURL)) {
  599. const processor = new PageProcessor(options);
  600. this.relImportProcessors.set(linkElement, processor);
  601. await processor.loadPage();
  602. return processor.initialize();
  603. }
  604. }
  605. } else {
  606. linkElement.setAttribute("href", EMPTY_DATA_URI);
  607. const processor = this.relImportProcessors.get(linkElement);
  608. if (processor) {
  609. this.stats.add("processed", "imports", 1);
  610. this.relImportProcessors.delete(linkElement);
  611. const pageData = await processor.getPageData();
  612. linkElement.setAttribute("href", "data:text/html," + pageData.content);
  613. this.stats.addAll(pageData);
  614. } else {
  615. this.stats.add("discarded", "imports", 1);
  616. }
  617. }
  618. }));
  619. }
  620. async attributeStyles(initialization) {
  621. await Promise.all(Array.from(this.doc.querySelectorAll("[style]")).map(async element => {
  622. let stylesheetContent = element.getAttribute("style");
  623. if (initialization) {
  624. stylesheetContent = DomProcessorHelper.resolveStylesheetURLs(stylesheetContent, this.baseURI);
  625. } else {
  626. stylesheetContent = await DomProcessorHelper.processStylesheet(element.getAttribute("style"), this.baseURI);
  627. }
  628. element.setAttribute("style", stylesheetContent);
  629. }));
  630. }
  631. async linkStylesheets() {
  632. await Promise.all(Array.from(this.doc.querySelectorAll("link[rel*=stylesheet]")).map(async linkElement => {
  633. const stylesheetContent = await DomProcessorHelper.resolveLinkStylesheetURLs(linkElement.href, this.baseURI, linkElement.media, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
  634. const styleElement = this.doc.createElement("style");
  635. styleElement.textContent = stylesheetContent;
  636. linkElement.parentElement.replaceChild(styleElement, linkElement);
  637. }));
  638. }
  639. }
  640. // ---------
  641. // DomHelper
  642. // ---------
  643. class DomProcessorHelper {
  644. static setFrameEmptySrc(frameElement) {
  645. if (frameElement.tagName == "OBJECT") {
  646. frameElement.setAttribute("data", "data:text/html,");
  647. } else {
  648. frameElement.setAttribute("srcdoc", "");
  649. frameElement.removeAttribute("src");
  650. }
  651. }
  652. static setFrameContent(frameElement, content) {
  653. if (frameElement.tagName == "OBJECT") {
  654. frameElement.setAttribute("data", "data:text/html," + content);
  655. } else {
  656. frameElement.setAttribute("srcdoc", content);
  657. frameElement.removeAttribute("src");
  658. }
  659. }
  660. static isolateElements(rootElement) {
  661. rootElement.querySelectorAll("*").forEach(element => {
  662. if (element.getAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME) == "") {
  663. element.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME);
  664. } else if (!element.querySelector("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]")) {
  665. element.remove();
  666. }
  667. });
  668. isolateParentElements(rootElement.parentElement, rootElement);
  669. function isolateParentElements(parentElement, element) {
  670. if (parentElement) {
  671. Array.from(parentElement.childNodes).forEach(node => {
  672. if (node != element && node.tagName != "HEAD" && node.tagName != "STYLE") {
  673. node.remove();
  674. }
  675. });
  676. }
  677. element = element.parentElement;
  678. if (element && element.parentElement) {
  679. isolateParentElements(element.parentElement, element);
  680. }
  681. }
  682. }
  683. static async resolveImportURLs(stylesheetContent, baseURI, options) {
  684. stylesheetContent = DomProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI);
  685. stylesheetContent = DomUtil.removeCssComments(stylesheetContent);
  686. const imports = DomUtil.getImportFunctions(stylesheetContent);
  687. await Promise.all(imports.map(async cssImport => {
  688. const match = DomUtil.matchImport(cssImport);
  689. if (match) {
  690. const resourceURL = DomUtil.normalizeURL(match.resourceURL);
  691. if (resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
  692. const styleSheetUrl = new URL(match.resourceURL, baseURI).href;
  693. let importedStylesheetContent = await Download.getContent(styleSheetUrl, { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
  694. importedStylesheetContent = DomUtil.wrapMediaQuery(importedStylesheetContent, match.media);
  695. if (stylesheetContent.includes(cssImport)) {
  696. importedStylesheetContent = await DomProcessorHelper.resolveImportURLs(importedStylesheetContent, styleSheetUrl, options);
  697. stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(cssImport), importedStylesheetContent);
  698. }
  699. }
  700. }
  701. }));
  702. return stylesheetContent;
  703. }
  704. static resolveStylesheetURLs(stylesheetContent, baseURI) {
  705. const urlFunctions = DomUtil.getUrlFunctions(stylesheetContent);
  706. urlFunctions.map(urlFunction => {
  707. const originalResourceURL = DomUtil.matchURL(urlFunction);
  708. const resourceURL = DomUtil.normalizeURL(originalResourceURL);
  709. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  710. const resolvedURL = new URL(resourceURL, baseURI).href;
  711. if (resourceURL != resolvedURL && stylesheetContent.includes(urlFunction)) {
  712. stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, resolvedURL));
  713. }
  714. } else {
  715. if (resourceURL.startsWith(DATA_URI_PREFIX)) {
  716. const escapedResourceURL = resourceURL.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
  717. if (escapedResourceURL != resourceURL && stylesheetContent.includes(urlFunction)) {
  718. stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, escapedResourceURL));
  719. }
  720. }
  721. }
  722. });
  723. return stylesheetContent;
  724. }
  725. static async resolveLinkStylesheetURLs(resourceURL, baseURI, media, options) {
  726. resourceURL = DomUtil.normalizeURL(resourceURL);
  727. if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
  728. let stylesheetContent = await Download.getContent(resourceURL, { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
  729. stylesheetContent = await DomProcessorHelper.resolveImportURLs(stylesheetContent, resourceURL, options);
  730. stylesheetContent = DomUtil.wrapMediaQuery(stylesheetContent, media);
  731. return stylesheetContent;
  732. }
  733. }
  734. static async processStylesheet(stylesheetContent, baseURI) {
  735. stylesheetContent = DomProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI);
  736. const urlFunctions = DomUtil.getUrlFunctions(stylesheetContent);
  737. await Promise.all(urlFunctions.map(async urlFunction => {
  738. const originalResourceURL = DomUtil.matchURL(urlFunction);
  739. const resourceURL = DomUtil.normalizeURL(originalResourceURL);
  740. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL) && stylesheetContent.includes(urlFunction)) {
  741. const dataURI = await batchRequest.addURL(resourceURL);
  742. stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, dataURI));
  743. }
  744. }));
  745. return stylesheetContent;
  746. }
  747. static async processAttribute(resourceElements, attributeName, baseURI) {
  748. await Promise.all(Array.from(resourceElements).map(async resourceElement => {
  749. let resourceURL = resourceElement.getAttribute(attributeName);
  750. if (resourceURL) {
  751. resourceURL = DomUtil.normalizeURL(resourceURL);
  752. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  753. try {
  754. const dataURI = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
  755. resourceElement.setAttribute(attributeName, dataURI);
  756. } catch (error) {
  757. /* ignored */
  758. }
  759. }
  760. }
  761. }));
  762. }
  763. static async processXLinks(resourceElements, baseURI) {
  764. await Promise.all(Array.from(resourceElements).map(async resourceElement => {
  765. const originalResourceURL = resourceElement.getAttribute("xlink:href");
  766. if (originalResourceURL) {
  767. const resourceURL = DomUtil.normalizeURL(originalResourceURL);
  768. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  769. try {
  770. const content = await batchRequest.addURL(new URL(resourceURL, baseURI).href, false);
  771. const DOMParser = DOM.getParser();
  772. if (DOMParser) {
  773. const svgDoc = new DOMParser().parseFromString(content, "image/svg+xml");
  774. const hashMatch = originalResourceURL.match(/(#.+?)$/);
  775. if (hashMatch && hashMatch[0]) {
  776. const symbolElement = svgDoc.querySelector(hashMatch[0]);
  777. if (symbolElement) {
  778. resourceElement.setAttribute("xlink:href", hashMatch[0]);
  779. resourceElement.parentElement.appendChild(symbolElement);
  780. }
  781. } else {
  782. resourceElement.setAttribute("xlink:href", "data:image/svg+xml," + content);
  783. }
  784. } else {
  785. resourceElement.setAttribute("xlink:href", "data:image/svg+xml," + content);
  786. }
  787. } catch (error) {
  788. /* ignored */
  789. }
  790. }
  791. }
  792. }));
  793. }
  794. static async processSrcset(resourceElements, attributeName, baseURI) {
  795. await Promise.all(Array.from(resourceElements).map(async resourceElement => {
  796. const srcset = DOM.parseSrcset(resourceElement.getAttribute(attributeName));
  797. const srcsetValues = await Promise.all(srcset.map(async srcsetValue => {
  798. const resourceURL = DomUtil.normalizeURL(srcsetValue.url);
  799. if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
  800. try {
  801. const dataURI = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
  802. return dataURI + (srcsetValue.w ? " " + srcsetValue.w + "w" : srcsetValue.d ? " " + srcsetValue.d + "x" : "");
  803. } catch (error) {
  804. /* ignored */
  805. }
  806. }
  807. }));
  808. resourceElement.setAttribute(attributeName, srcsetValues.join(", "));
  809. }));
  810. }
  811. }
  812. // -------
  813. // DomUtil
  814. // -------
  815. const DATA_URI_PREFIX = "data:";
  816. const BLOB_URI_PREFIX = "blob:";
  817. const HTTP_URI_PREFIX = /^https?:\/\//;
  818. const ABOUT_BLANK_URI = "about:blank";
  819. const NOT_EMPTY_URL = /^https?:\/\/.+/;
  820. const REGEXP_URL_FN = /(url\s*\(\s*'(.*?)'\s*\))|(url\s*\(\s*"(.*?)"\s*\))|(url\s*\(\s*(.*?)\s*\))/gi;
  821. const REGEXP_URL_SIMPLE_QUOTES_FN = /^url\s*\(\s*'(.*?)'\s*\)$/i;
  822. const REGEXP_URL_DOUBLE_QUOTES_FN = /^url\s*\(\s*"(.*?)"\s*\)$/i;
  823. const REGEXP_URL_NO_QUOTES_FN = /^url\s*\(\s*(.*?)\s*\)$/i;
  824. const REGEXP_IMPORT_FN = /(@import\s*url\s*\(\s*'(.*?)'\s*\)\s*(.*?);?)|(@import\s*url\s*\(\s*"(.*?)"\s*\)\s*(.*?);?)|(@import\s*url\s*\(\s*(.*?)\s*\)\s*(.*?);?)|(@import\s*'(.*?)'\s*(.*?);?)|(@import\s*"(.*?)"\s*(.*?);?)|(@import\s*(.*?)\s*(.*?);?)/gi;
  825. const REGEXP_IMPORT_URL_SIMPLE_QUOTES_FN = /@import\s*url\s*\(\s*'(.*?)'\s*\)\s*(.*?)/i;
  826. const REGEXP_IMPORT_URL_DOUBLE_QUOTES_FN = /@import\s*url\s*\(\s*"(.*?)"\s*\)\s*(.*?)/i;
  827. const REGEXP_IMPORT_URL_NO_QUOTES_FN = /@import\s*url\s*\(\s*(.*?)\s*\)\s*(.*?)/i;
  828. const REGEXP_IMPORT_SIMPLE_QUOTES_FN = /@import\s*'(.*?)'\s*(.*?)/i;
  829. const REGEXP_IMPORT_DOUBLE_QUOTES_FN = /@import\s*"(.*?)"\s*(.*?)/i;
  830. const REGEXP_IMPORT_NO_QUOTES_FN = /@import\s*(.*?)\s*(.*?)/i;
  831. class DomUtil {
  832. static normalizeURL(url) {
  833. if (url.startsWith(DATA_URI_PREFIX)) {
  834. return url;
  835. } else {
  836. return url.split("#")[0];
  837. }
  838. }
  839. static getRegExp(string) {
  840. return new RegExp(string.replace(/([{}()^$&.*?/+|[\\\\]|\]|-)/g, "\\$1"), "gi");
  841. }
  842. static getUrlFunctions(stylesheetContent) {
  843. return Array.from(new Set(stylesheetContent.match(REGEXP_URL_FN) || []));
  844. }
  845. static getImportFunctions(stylesheetContent) {
  846. return stylesheetContent.match(REGEXP_IMPORT_FN) || [];
  847. }
  848. static matchURL(stylesheetContent) {
  849. const match = stylesheetContent.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
  850. stylesheetContent.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
  851. stylesheetContent.match(REGEXP_URL_NO_QUOTES_FN);
  852. return match && match[1];
  853. }
  854. static testValidPath(resourceURL) {
  855. return !resourceURL.startsWith(DATA_URI_PREFIX) && !resourceURL.startsWith(BLOB_URI_PREFIX) && resourceURL != ABOUT_BLANK_URI && (!resourceURL.match(HTTP_URI_PREFIX) || resourceURL.match(NOT_EMPTY_URL));
  856. }
  857. static matchImport(stylesheetContent) {
  858. const match = stylesheetContent.match(REGEXP_IMPORT_URL_SIMPLE_QUOTES_FN) ||
  859. stylesheetContent.match(REGEXP_IMPORT_URL_DOUBLE_QUOTES_FN) ||
  860. stylesheetContent.match(REGEXP_IMPORT_URL_NO_QUOTES_FN) ||
  861. stylesheetContent.match(REGEXP_IMPORT_SIMPLE_QUOTES_FN) ||
  862. stylesheetContent.match(REGEXP_IMPORT_DOUBLE_QUOTES_FN) ||
  863. stylesheetContent.match(REGEXP_IMPORT_NO_QUOTES_FN);
  864. if (match) {
  865. const [, resourceURL, media] = match;
  866. return { resourceURL, media };
  867. }
  868. }
  869. static removeCssComments(stylesheetContent) {
  870. let start, end;
  871. do {
  872. start = stylesheetContent.indexOf("/*");
  873. end = stylesheetContent.indexOf("*/", start);
  874. if (start != -1 && end != -1) {
  875. stylesheetContent = stylesheetContent.substring(0, start) + stylesheetContent.substr(end + 2);
  876. }
  877. } while (start != -1 && end != -1);
  878. return stylesheetContent;
  879. }
  880. static wrapMediaQuery(stylesheetContent, mediaQuery) {
  881. if (mediaQuery) {
  882. return "@media " + mediaQuery + "{ " + stylesheetContent + " }";
  883. } else {
  884. return stylesheetContent;
  885. }
  886. }
  887. }
  888. // -----
  889. // Stats
  890. // -----
  891. const STATS_DEFAULT_VALUES = {
  892. discarded: {
  893. htmlBytes: 0,
  894. hiddenElements: 0,
  895. imports: 0,
  896. scripts: 0,
  897. objects: 0,
  898. audioSource: 0,
  899. videoSource: 0,
  900. frames: 0,
  901. cssRules: 0
  902. },
  903. processed: {
  904. htmlBytes: 0,
  905. imports: 0,
  906. scripts: 0,
  907. frames: 0,
  908. cssRules: 0,
  909. canvas: 0,
  910. styleSheets: 0,
  911. resources: 0
  912. }
  913. };
  914. class Stats {
  915. constructor(options) {
  916. this.options = options;
  917. if (options.displayStats) {
  918. this.data = JSON.parse(JSON.stringify(STATS_DEFAULT_VALUES));
  919. }
  920. }
  921. set(type, subType, value) {
  922. if (this.options.displayStats) {
  923. this.data[type][subType] = value;
  924. }
  925. }
  926. add(type, subType, value) {
  927. if (this.options.displayStats) {
  928. this.data[type][subType] += value;
  929. }
  930. }
  931. addAll(pageData) {
  932. if (this.options.displayStats) {
  933. Object.keys(this.data.discarded).forEach(key => this.add("discarded", key, pageData.stats.discarded[key] || 0));
  934. Object.keys(this.data.processed).forEach(key => this.add("processed", key, pageData.stats.processed[key] || 0));
  935. }
  936. }
  937. }
  938. return { getClass };
  939. })();