| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989 |
- /*
- * Copyright 2018 Gildas Lormeau
- * contact : gildas.lormeau <at> gmail.com
- *
- * This file is part of SingleFile.
- *
- * SingleFile is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * SingleFile is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
- */
- /* global Blob */
- this.SingleFileCore = this.SingleFileCore || (() => {
- const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
- const SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME = "data-single-file-selected-content-root";
- const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
- const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
- const WIN_ID_ATTRIBUTE_NAME = "data-frame-tree-win-id";
- let Download, DOM, URL;
- function getClass(...args) {
- [Download, DOM, URL] = args;
- return class {
- constructor(options) {
- this.options = options;
- this.SELECTED_CONTENT_ATTRIBUTE_NAME = SELECTED_CONTENT_ATTRIBUTE_NAME;
- this.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME = SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME;
- }
- async initialize() {
- this.processor = new PageProcessor(this.options);
- await this.processor.loadPage();
- await this.processor.initialize();
- }
- async preparePageData() {
- await this.processor.preparePageData();
- }
- getPageData() {
- return this.processor.getPageData();
- }
- };
- }
- // -------------
- // ProgressEvent
- // -------------
- const PAGE_LOADING = "page-loading";
- const PAGE_LOADED = "page-loaded";
- const RESOURCES_INITIALIZING = "resource-initializing";
- const RESOURCES_INITIALIZED = "resources-initialized";
- const RESOURCE_LOADED = "resource-loaded";
- const PAGE_ENDED = "page-ended";
- class ProgressEvent {
- constructor(type, details) {
- return { type, details, PAGE_LOADING, PAGE_LOADED, RESOURCES_INITIALIZING, RESOURCES_INITIALIZED, RESOURCE_LOADED, PAGE_ENDED };
- }
- }
- // -------------
- // PageProcessor
- // -------------
- class PageProcessor {
- constructor(options) {
- this.options = options;
- this.options.content = this.options.content || (this.options.doc ? DOMProcessor.serialize(this.options.doc, false) : null);
- this.options.url = this.options.url || this.options.doc.location.href;
- this.processor = new DOMProcessor(options);
- if (this.options.doc) {
- this.options.canvasData = this.processor.getCanvasData();
- this.options.emptyStyleRulesText = this.processor.getEmptyStyleRulesText();
- this.processor.fixInlineScripts();
- this.processor.disableNoscriptElements();
- this.processor.hideNonMetadataContents();
- if (this.options.removeHiddenElements) {
- this.processor.markRemovedElements();
- }
- if (this.options.compressHTML) {
- this.processor.markPreservedElements();
- }
- }
- this.onprogress = options.onprogress || (() => { });
- }
- async loadPage() {
- this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
- await this.processor.loadPage(this.options.content);
- this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
- }
- async initialize() {
- this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
- this.processor.removeInfoToolbar();
- this.processor.enableDisabledNoscriptTags(this.processor.doc.head.querySelectorAll("disabled-noscript"));
- this.processor.replaceEmptyStyles();
- if (!this.options.jsEnabled || (this.options.saveRawPage && this.options.removeScripts)) {
- this.processor.insertNoscriptContents();
- }
- if (this.options.removeFrames) {
- this.processor.removeFrames();
- }
- if (this.options.removeImports) {
- this.processor.removeImports();
- }
- if (this.options.removeScripts) {
- this.processor.removeScripts();
- }
- this.processor.removeDiscardedResources();
- this.processor.resetCharsetMeta();
- if (this.options.compressHTML) {
- this.processor.compressHTML();
- }
- if (this.options.insertFaviconLink) {
- this.processor.insertFaviconLink();
- }
- this.processor.resolveHrefs();
- this.processor.replaceCanvasElements();
- if (this.options.removeHiddenElements) {
- this.processor.removeHiddenElements();
- }
- const initializationPromises = [this.processor.inlineStylesheets(true), this.processor.linkStylesheets(), this.processor.attributeStyles(true)];
- if (!this.options.removeFrames) {
- initializationPromises.push(this.processor.frames(true));
- }
- if (!this.options.removeImports) {
- initializationPromises.push(this.processor.htmlImports(true));
- }
- await Promise.all(initializationPromises);
- if (this.options.compressHTML) {
- this.processor.compressHTML();
- }
- if (this.options.removeUnusedCSSRules) {
- this.processor.removeUnusedCSSRules();
- }
- this.pendingPromises = [this.processor.inlineStylesheets(), this.processor.attributeStyles(), this.processor.pageResources()];
- if (!this.options.removeScripts) {
- this.pendingPromises.push(this.processor.scripts());
- }
- if (this.options.doc) {
- this.processor.enableDisabledNoscriptTags(this.options.doc.querySelectorAll("disabled-noscript"));
- this.processor.displayHiddenNonMetadataContents();
- if (this.options.removeHiddenElements) {
- this.processor.unmarkRemovedElements();
- }
- if (this.options.compressHTML) {
- this.processor.unmarkPreservedElements();
- }
- if (!this.options.removeFrames) {
- this.processor.removeWindowIdFrames();
- }
- this.options.doc = null;
- this.options.win = null;
- }
- this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() }));
- }
- async preparePageData() {
- await this.processor.retrieveResources(
- details => {
- details.pageURL = this.options.url;
- this.onprogress(new ProgressEvent(RESOURCE_LOADED, details));
- });
- await this.pendingPromises;
- if (this.options.lazyLoadImages) {
- this.processor.lazyLoadImages();
- }
- if (!this.options.removeFrames) {
- await this.processor.frames();
- }
- if (!this.options.removeImports) {
- await this.processor.htmlImports();
- }
- if (this.options.compressHTML) {
- this.processor.compressHTML(true);
- }
- if (this.options.insertSingleFileComment) {
- this.processor.insertSingleFileCommentNode();
- }
- this.processor.removeBase();
- }
- getPageData() {
- this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
- return this.processor.getPageData();
- }
- }
- // --------
- // BatchRequest
- // --------
- class BatchRequest {
- constructor() {
- this.requests = new Map();
- }
- async addURL(resourceURL) {
- return new Promise((resolve, reject) => {
- const resourceRequests = this.requests.get(resourceURL);
- if (resourceRequests) {
- resourceRequests.push({ resolve, reject });
- } else {
- this.requests.set(resourceURL, [{ resolve, reject }]);
- }
- });
- }
- getMaxResources() {
- return Array.from(this.requests.keys()).length;
- }
- async run(onloadListener, options) {
- const resourceURLs = Array.from(this.requests.keys());
- let indexResource = 0;
- return Promise.all(resourceURLs.map(async resourceURL => {
- const resourceRequests = this.requests.get(resourceURL);
- try {
- const dataURI = await Download.getContent(resourceURL, { asDataURI: true, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
- resourceRequests.forEach(resourceRequest => resourceRequest.resolve(dataURI));
- } catch (error) {
- resourceRequests.forEach(resourceRequest => resourceRequest.reject(error));
- }
- this.requests.delete(resourceURL);
- indexResource = indexResource + 1;
- onloadListener({ index: indexResource, max: resourceURLs.length, url: resourceURL });
- }));
- }
- }
- // ------------
- // DOMProcessor
- // ------------
- const ESCAPED_FRAGMENT = "_escaped_fragment_=";
- const EMPTY_DATA_URI = "data:base64,";
- const batchRequest = new BatchRequest();
- class DOMProcessor {
- constructor(options) {
- this.options = options;
- if (this.options.displayStats) {
- this.stats = {
- discarded: {
- htmlBytes: 0,
- hiddenElements: 0,
- imports: 0,
- scripts: 0,
- objects: 0,
- audioSource: 0,
- videoSource: 0,
- frames: 0,
- cssRules: 0,
- canvas: 0,
- styleSheets: 0,
- resources: 0
- },
- processed: {
- htmlBytes: 0,
- hiddenElements: 0,
- imports: 0,
- scripts: 0,
- objects: 0,
- audioSource: 0,
- videoSource: 0,
- frames: 0,
- cssRules: 0,
- canvas: 0,
- styleSheets: 0,
- resources: 0
- }
- };
- }
- this.baseURI = options.url;
- }
- static serialize(doc, compressHTML) {
- return DOM.serialize(doc, compressHTML);
- }
- async loadPage(pageContent) {
- if (!pageContent || this.options.saveRawPage) {
- pageContent = await Download.getContent(this.baseURI, { asDataURI: false, maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
- }
- this.dom = DOM.create(pageContent, this.baseURI);
- this.DOMParser = this.dom.DOMParser;
- this.doc = this.dom.document;
- if (!pageContent && this.doc.querySelector("meta[name=fragment][content=\"!\"]") && !this.baseURI.endsWith("?" + ESCAPED_FRAGMENT) && !this.baseURI.endsWith("&" + ESCAPED_FRAGMENT)) {
- await DOMProcessor.loadEscapedFragmentPage();
- }
- }
- async loadEscapedFragmentPage() {
- if (this.baseURI.includes("?")) {
- this.baseURI += "&";
- } else {
- this.baseURI += "?";
- }
- this.baseURI += ESCAPED_FRAGMENT;
- await this.loadPage();
- }
- async retrieveResources(onloadListener) {
- if (this.options.displayStats) {
- this.stats.processed.resources = batchRequest.getMaxResources();
- }
- await batchRequest.run(onloadListener, this.options);
- }
- getPageData() {
- if (this.options.selected) {
- const rootElement = this.doc.querySelector("[" + SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME + "]");
- if (rootElement) {
- DomProcessorHelper.isolateElements(rootElement);
- rootElement.removeAttribute(SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME);
- rootElement.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME);
- }
- }
- const titleElement = this.doc.querySelector("title");
- let title;
- if (titleElement) {
- title = titleElement.textContent.trim();
- }
- const matchTitle = this.baseURI.match(/([^/]*)\/?(\.html?.*)$/) || this.baseURI.match(/\/\/([^/]*)\/?$/);
- let size;
- if (this.options.displayStats) {
- size = new Blob([this.doc.documentElement.outerHTML]).size;
- }
- const content = DOMProcessor.serialize(this.doc, this.options.compressHTML);
- if (this.options.displayStats) {
- this.stats.processed.htmlBytes = new Blob([content]).size;
- this.stats.discarded.htmlBytes += size - this.stats.processed.htmlBytes;
- }
- return {
- stats: this.stats,
- title: title || (this.baseURI && matchTitle ? matchTitle[1] : ""),
- content
- };
- }
- fixInlineScripts() {
- this.options.doc.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
- }
- disableNoscriptElements() {
- this.options.doc.head.querySelectorAll("noscript").forEach(element => {
- const disabledNoscriptElement = this.options.doc.createElement("disabled-noscript");
- Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
- disabledNoscriptElement.hidden = true;
- element.parentElement.replaceChild(disabledNoscriptElement, element);
- });
- }
- hideNonMetadataContents() {
- this.options.doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.hidden = true);
- }
- markRemovedElements() {
- this.options.doc.querySelectorAll("html > body *:not(style):not(script):not(link):not(frame):not(iframe):not(object)").forEach(element => {
- const style = this.options.win.getComputedStyle(element);
- if (element instanceof this.options.win.HTMLElement && (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) && !element.querySelector("iframe, frame, object[type=\"text/html\"][data]")) {
- element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
- }
- });
- }
- markPreservedElements() {
- this.options.doc.querySelectorAll("*").forEach(element => {
- const style = this.options.win.getComputedStyle(element);
- if (style.whiteSpace.startsWith("pre")) {
- element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
- }
- });
- }
- displayHiddenNonMetadataContents() {
- this.options.doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
- }
- unmarkPreservedElements() {
- this.options.doc.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
- }
- unmarkRemovedElements() {
- this.options.doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
- }
- removeWindowIdFrames() {
- this.options.doc.querySelectorAll("[" + WIN_ID_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(WIN_ID_ATTRIBUTE_NAME));
- }
- getEmptyStyleRulesText() {
- const textData = [];
- this.options.doc.querySelectorAll("style").forEach(styleElement => {
- if (!styleElement.textContent) {
- textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
- }
- });
- return textData;
- }
- getCanvasData() {
- const canvasData = [];
- this.options.doc.querySelectorAll("canvas").forEach(canvasElement => {
- try {
- canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
- } catch (error) {
- canvasData.push(null);
- }
- });
- return canvasData;
- }
- enableDisabledNoscriptTags(noscriptTags) {
- noscriptTags.forEach(element => {
- const noscriptElement = this.options.doc.createElement("noscript");
- Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
- element.parentElement.replaceChild(noscriptElement, element);
- });
- }
- insertNoscriptContents() {
- if (this.DOMParser) {
- this.doc.querySelectorAll("noscript").forEach(element => {
- const fragment = this.doc.createDocumentFragment();
- Array.from(element.childNodes).forEach(node => {
- const parsedNode = new this.DOMParser().parseFromString(node.nodeValue, "text/html");
- Array.from(parsedNode.head.childNodes).concat(Array.from(parsedNode.body.childNodes)).forEach(node => {
- this.doc.importNode(node);
- fragment.appendChild(node);
- });
- });
- element.parentElement.replaceChild(fragment, element);
- });
- }
- }
- lazyLoadImages() {
- this.dom.lazyLoader.process(this.doc);
- }
- removeDiscardedResources() {
- 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\"]), link[rel*=preload], link[rel*=prefetch]");
- if (this.options.displayStats) {
- this.stats.discarded.objects = objectElements.length;
- }
- objectElements.forEach(element => element.remove());
- this.doc.querySelectorAll("[onload]").forEach(element => element.removeAttribute("onload"));
- this.doc.querySelectorAll("[onerror]").forEach(element => element.removeAttribute("onerror"));
- if (this.options.removeAudioSrc) {
- const audioSourceElements = this.doc.querySelectorAll("audio[src], audio > source[src]");
- if (this.options.displayStats) {
- this.stats.discarded.audioSource = objectElements.length;
- }
- audioSourceElements.forEach(element => element.removeAttribute("src"));
- }
- if (this.options.removeVideoSrc) {
- const videoSourceElements = this.doc.querySelectorAll("video[src], video > source[src]");
- if (this.options.displayStats) {
- this.stats.discarded.videoSource = objectElements.length;
- }
- videoSourceElements.forEach(element => element.removeAttribute("src"));
- }
- }
- removeBase() {
- this.doc.querySelectorAll("base").forEach(element => element.remove());
- }
- removeInfoToolbar() {
- this.doc.querySelectorAll("singlefile-infobar").forEach(element => element.remove());
- }
- removeScripts() {
- const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"])");
- if (this.options.displayStats) {
- this.stats.discarded.scripts = scriptElements.length;
- }
- scriptElements.forEach(element => element.remove());
- }
- removeFrames() {
- const frameElements = this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
- if (this.options.displayStats) {
- this.stats.discarded.frames = frameElements.length;
- }
- this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]").forEach(element => element.remove());
- }
- removeImports() {
- const importElements = this.doc.querySelectorAll("link[rel=import]");
- if (this.options.displayStats) {
- this.stats.discarded.imports = importElements.length;
- }
- importElements.forEach(element => element.remove());
- }
- resetCharsetMeta() {
- this.doc.querySelectorAll("meta[charset]").forEach(element => element.remove());
- const metaElement = this.doc.createElement("meta");
- metaElement.setAttribute("charset", "utf-8");
- this.doc.head.insertBefore(metaElement, this.doc.head.firstElementChild);
- }
- insertFaviconLink() {
- let faviconElement = this.doc.querySelectorAll("link[href][rel*=\"icon\"]")[0];
- if (!faviconElement) {
- faviconElement = this.doc.createElement("link");
- faviconElement.setAttribute("type", "image/x-icon");
- faviconElement.setAttribute("rel", "shortcut icon");
- faviconElement.setAttribute("href", "/favicon.ico");
- this.doc.head.appendChild(faviconElement);
- }
- }
- resolveHrefs() {
- this.doc.querySelectorAll("[href]").forEach(element => {
- const match = element.href && element.href.match(/(.*)#.*$/);
- if (!match || match[1] != this.baseURI) {
- element.setAttribute("href", element.href);
- }
- });
- }
- removeUnusedCSSRules() {
- const stats = this.dom.rulesMinifier(this.doc);
- if (this.options.displayStats) {
- this.stats.processed.cssRules = stats.processed;
- this.stats.discarded.cssRules = stats.discarded;
- }
- }
- removeHiddenElements() {
- const hiddenElements = this.doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]");
- if (this.options.displayStats) {
- this.stats.discarded.hiddenElements = hiddenElements.length;
- }
- hiddenElements.forEach(element => element.remove());
- }
- compressHTML(postProcess) {
- if (postProcess) {
- let size;
- if (this.options.displayStats) {
- size = new Blob([this.doc.documentElement.outerHTML]).size;
- }
- this.dom.htmlmini.postProcess(this.doc);
- if (this.options.displayStats) {
- this.stats.discarded.htmlBytes += size - (new Blob([this.doc.documentElement.outerHTML]).size);
- }
- } else {
- let size;
- if (this.options.displayStats) {
- size = new Blob([this.doc.documentElement.outerHTML]).size;
- }
- this.dom.htmlmini.process(this.doc, { preservedSpaceAttributeName: PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME });
- if (this.options.displayStats) {
- this.stats.discarded.htmlBytes += size - (new Blob([this.doc.documentElement.outerHTML]).size);
- }
- this.doc.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
- }
- }
- insertSingleFileCommentNode() {
- const commentNode = this.doc.createComment("\n Archive processed by SingleFile \n url: " + this.baseURI + " \n saved date: " + new Date() + " \n");
- this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
- }
- replaceCanvasElements() {
- if (this.options.canvasData) {
- this.doc.querySelectorAll("canvas").forEach((canvasElement, indexCanvasElement) => {
- const canvasData = this.options.canvasData[indexCanvasElement];
- if (canvasData) {
- const imgElement = this.doc.createElement("img");
- imgElement.setAttribute("src", canvasData.dataURI);
- Array.from(canvasElement.attributes).forEach(attribute => {
- if (attribute.value) {
- imgElement.setAttribute(attribute.name, attribute.value);
- }
- });
- if (!imgElement.width && canvasData.width) {
- imgElement.style.pixelWidth = canvasData.width;
- }
- if (!imgElement.height && canvasData.height) {
- imgElement.style.pixelHeight = canvasData.height;
- }
- canvasElement.parentElement.replaceChild(imgElement, canvasElement);
- if (this.options.displayStats) {
- this.stats.processed.canvas++;
- }
- }
- });
- }
- }
- replaceEmptyStyles() {
- if (this.options.emptyStyleRulesText) {
- let indexStyle = 0;
- this.doc.querySelectorAll("style").forEach(styleElement => {
- if (!styleElement.textContent) {
- styleElement.textContent = this.options.emptyStyleRulesText[indexStyle];
- indexStyle++;
- }
- });
- }
- }
- async pageResources() {
- const resourcePromises = [
- DomProcessorHelper.processAttribute(this.doc.querySelectorAll("link[href][rel*=\"icon\"]"), "href", this.baseURI),
- DomProcessorHelper.processAttribute(this.doc.querySelectorAll("img[src], input[src][type=image], object[type=\"image/svg+xml\"], object[type=\"image/svg-xml\"], embed[src*=\".svg\"]"), "src", this.baseURI),
- DomProcessorHelper.processAttribute(this.doc.querySelectorAll("video[poster]"), "poster", this.baseURI),
- DomProcessorHelper.processAttribute(this.doc.querySelectorAll("*[background]"), "background", this.baseURI),
- DomProcessorHelper.processAttribute(this.doc.querySelectorAll("image, use"), "xlink:href", this.baseURI),
- DomProcessorHelper.processSrcset(this.doc.querySelectorAll("[srcset]"), "srcset", this.baseURI, this.dom.parseSrcset)
- ];
- if (!this.options.removeAudioSrc) {
- resourcePromises.push(DomProcessorHelper.processAttribute(this.doc.querySelectorAll("audio[src], audio > source[src]"), "src", this.baseURI));
- }
- if (!this.options.removeVideoSrc) {
- resourcePromises.push(DomProcessorHelper.processAttribute(this.doc.querySelectorAll("video[src], video > source[src]"), "src", this.baseURI));
- }
- if (this.options.lazyLoadImages) {
- const imageSelectors = this.dom.lazyLoader.imageSelectors;
- Object.keys(imageSelectors.src).forEach(selector => resourcePromises.push(DomProcessorHelper.processAttribute(this.doc.querySelectorAll(selector), imageSelectors.src[selector], this.baseURI)));
- Object.keys(imageSelectors.srcset).forEach(selector => resourcePromises.push(DomProcessorHelper.processSrcset(this.doc.querySelectorAll(selector), imageSelectors.srcset[selector], this.baseURI, this.dom.parseSrcset)));
- }
- await resourcePromises;
- }
- async inlineStylesheets(initialization) {
- await Promise.all(Array.from(this.doc.querySelectorAll("style")).map(async styleElement => {
- if (!initialization && this.options.displayStats) {
- this.stats.processed.styleSheets++;
- }
- const stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(styleElement.textContent, this.baseURI, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled }) : await DomProcessorHelper.processStylesheet(styleElement.textContent, this.baseURI);
- styleElement.textContent = !initialization && this.options.compressCSS ? this.dom.uglifycss(stylesheetContent) : stylesheetContent;
- }));
- }
- async scripts() {
- await Promise.all(Array.from(this.doc.querySelectorAll("script[src]")).map(async scriptElement => {
- if (scriptElement.src) {
- if (this.options.displayStats) {
- this.stats.processed.scripts++;
- }
- const scriptContent = await Download.getContent(scriptElement.src, { asDataURI: false, maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
- scriptElement.textContent = scriptContent.replace(/<\/script>/gi, "<\\/script>");
- }
- scriptElement.removeAttribute("src");
- }));
- }
- async frames(initialization) {
- const frameElements = Array.from(this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"));
- await Promise.all(frameElements.map(async frameElement => {
- DomProcessorHelper.setFrameEmptySrc(frameElement);
- frameElement.setAttribute("sandbox", "");
- const frameWindowId = frameElement.getAttribute(WIN_ID_ATTRIBUTE_NAME);
- if (frameWindowId) {
- const frameData = this.options.framesData.find(frame => frame.windowId == frameWindowId);
- if (frameData) {
- if (initialization) {
- const options = Object.create(this.options);
- options.insertSingleFileComment = false;
- options.insertFaviconLink = false;
- options.url = frameData.baseURI;
- options.windowId = frameWindowId;
- if (frameData.content) {
- options.content = frameData.content;
- options.canvasData = frameData.canvasData;
- options.emptyStyleRulesText = frameData.emptyStyleRulesText;
- frameData.processor = new PageProcessor(options);
- frameData.frameElement = frameElement;
- await frameData.processor.loadPage();
- return frameData.processor.initialize();
- }
- } else {
- if (frameData.processor) {
- if (this.options.displayStats) {
- this.stats.processed.frames++;
- }
- await frameData.processor.preparePageData();
- const pageData = await frameData.processor.getPageData();
- frameElement.removeAttribute(WIN_ID_ATTRIBUTE_NAME);
- DomProcessorHelper.setFrameContent(frameElement, pageData.content);
- if (this.options.displayStats) {
- Object.keys(this.stats.discarded).forEach(key => this.stats.discarded[key] += (pageData.stats.discarded[key] || 0));
- Object.keys(this.stats.processed).forEach(key => this.stats.processed[key] += (pageData.stats.processed[key] || 0));
- }
- } else if (this.options.displayStats) {
- this.stats.discarded.frames++;
- }
- }
- }
- }
- }));
- }
- async htmlImports(initialization) {
- const linkElements = Array.from(this.doc.querySelectorAll("link[rel=import][href]"));
- if (!this.relImportProcessors) {
- this.relImportProcessors = new Map();
- }
- await Promise.all(linkElements.map(async linkElement => {
- if (initialization) {
- const resourceURL = linkElement.href;
- const options = Object.create(this.options);
- options.insertSingleFileComment = false;
- options.insertFaviconLink = false;
- options.url = resourceURL;
- if (resourceURL) {
- if (resourceURL && resourceURL != this.baseURI && DomUtil.testValidPath(resourceURL)) {
- const processor = new PageProcessor(options);
- this.relImportProcessors.set(linkElement, processor);
- await processor.loadPage();
- return processor.initialize();
- }
- }
- } else {
- linkElement.setAttribute("href", EMPTY_DATA_URI);
- const processor = this.relImportProcessors.get(linkElement);
- if (processor) {
- if (this.options.displayStats) {
- this.stats.processed.imports++;
- }
- this.relImportProcessors.delete(linkElement);
- const pageData = await processor.getPageData();
- linkElement.setAttribute("href", "data:text/html," + pageData.content);
- if (this.options.displayStats) {
- Object.keys(this.stats.discarded).forEach(key => this.stats.discarded[key] += (pageData.stats.discarded[key] || 0));
- Object.keys(this.stats.processed).forEach(key => this.stats.processed[key] += (pageData.stats.processed[key] || 0));
- }
- } else if (this.options.displayStats) {
- this.stats.discarded.imports++;
- }
- }
- }));
- }
- async attributeStyles(initialization) {
- await Promise.all(Array.from(this.doc.querySelectorAll("[style]")).map(async element => {
- const stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(element.getAttribute("style"), this.baseURI, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled }) : await DomProcessorHelper.processStylesheet(element.getAttribute("style"), this.baseURI);
- element.setAttribute("style", this.options.compressCSS ? this.dom.uglifycss(stylesheetContent) : stylesheetContent);
- }));
- }
- async linkStylesheets() {
- await Promise.all(Array.from(this.doc.querySelectorAll("link[rel*=stylesheet]")).map(async linkElement => {
- const stylesheetContent = await DomProcessorHelper.resolveLinkStylesheetURLs(linkElement.href, this.baseURI, linkElement.media, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
- const styleElement = this.doc.createElement("style");
- styleElement.textContent = stylesheetContent;
- linkElement.parentElement.replaceChild(styleElement, linkElement);
- }));
- }
- }
- // ---------
- // DomHelper
- // ---------
- class DomProcessorHelper {
- static setFrameEmptySrc(frameElement) {
- if (frameElement.tagName == "OBJECT") {
- frameElement.setAttribute("data", "data:text/html,");
- } else {
- frameElement.setAttribute("srcdoc", "");
- frameElement.removeAttribute("src");
- }
- }
- static setFrameContent(frameElement, content) {
- if (frameElement.tagName == "OBJECT") {
- frameElement.setAttribute("data", "data:text/html," + content);
- } else {
- frameElement.setAttribute("srcdoc", content);
- frameElement.removeAttribute("src");
- }
- }
- static isolateElements(rootElement) {
- rootElement.querySelectorAll("*").forEach(element => {
- if (element.getAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME) == "") {
- element.removeAttribute(SELECTED_CONTENT_ATTRIBUTE_NAME);
- } else if (!element.querySelector("[" + SELECTED_CONTENT_ATTRIBUTE_NAME + "]")) {
- element.remove();
- }
- });
- isolateParentElements(rootElement.parentElement, rootElement);
- function isolateParentElements(parentElement, element) {
- if (parentElement) {
- Array.from(parentElement.childNodes).forEach(node => {
- if (node != element && node.tagName != "HEAD" && node.tagName != "STYLE") {
- node.remove();
- }
- });
- }
- element = element.parentElement;
- if (element && element.parentElement) {
- isolateParentElements(element.parentElement, element);
- }
- }
- }
- static async resolveImportURLs(stylesheetContent, baseURI, options) {
- stylesheetContent = DomUtil.removeCssComments(stylesheetContent);
- const imports = DomUtil.getImportFunctions(stylesheetContent);
- await Promise.all(imports.map(async cssImport => {
- const match = DomUtil.matchImport(cssImport);
- if (match) {
- const resourceURL = DomUtil.normalizeURL(match.resourceURL);
- if (resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
- let importedStylesheetContent = await Download.getContent(new URL(match.resourceURL, baseURI).href, { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
- importedStylesheetContent = DomUtil.wrapMediaQuery(importedStylesheetContent, match.media);
- if (stylesheetContent.indexOf(cssImport) != -1) {
- stylesheetContent = stylesheetContent.replace(cssImport, importedStylesheetContent);
- }
- }
- }
- }));
- stylesheetContent = DomProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI);
- if (imports.length) {
- return await DomProcessorHelper.resolveImportURLs(stylesheetContent, baseURI, options);
- } else {
- return stylesheetContent;
- }
- }
- static resolveStylesheetURLs(stylesheetContent, baseURI) {
- const urlFunctions = DomUtil.getUrlFunctions(stylesheetContent);
- urlFunctions.map(urlFunction => {
- let resourceURL = DomUtil.matchURL(urlFunction);
- resourceURL = DomUtil.normalizeURL(resourceURL);
- if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
- stylesheetContent = stylesheetContent.replace(urlFunction, urlFunction.replace(resourceURL, new URL(resourceURL, baseURI).href));
- }
- });
- return stylesheetContent;
- }
- static async resolveLinkStylesheetURLs(resourceURL, baseURI, media, options) {
- resourceURL = DomUtil.normalizeURL(resourceURL);
- if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
- let stylesheetContent = await Download.getContent(resourceURL, { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
- stylesheetContent = await DomProcessorHelper.resolveImportURLs(stylesheetContent, resourceURL, options);
- stylesheetContent = DomUtil.wrapMediaQuery(stylesheetContent, media);
- return stylesheetContent;
- }
- }
- static async processStylesheet(stylesheetContent, baseURI) {
- const urlFunctions = DomUtil.getUrlFunctions(stylesheetContent);
- await Promise.all(urlFunctions.map(async urlFunction => {
- let resourceURL = DomUtil.matchURL(urlFunction);
- resourceURL = DomUtil.normalizeURL(resourceURL);
- if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
- const dataURI = await batchRequest.addURL(resourceURL);
- stylesheetContent = stylesheetContent.replace(urlFunction, urlFunction.replace(resourceURL, dataURI));
- }
- }));
- return stylesheetContent;
- }
- static async processAttribute(resourceElements, attributeName, baseURI) {
- await Promise.all(Array.from(resourceElements).map(async resourceElement => {
- let resourceURL = resourceElement.getAttribute(attributeName);
- if (resourceURL) {
- resourceURL = DomUtil.normalizeURL(resourceURL);
- if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
- try {
- const dataURI = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
- resourceElement.setAttribute(attributeName, dataURI);
- } catch (error) {
- /* ignored */
- }
- }
- }
- }));
- }
- static async processSrcset(resourceElements, attributeName, baseURI, parseSrcset) {
- await Promise.all(Array.from(resourceElements).map(async resourceElement => {
- const srcset = parseSrcset(resourceElement.getAttribute(attributeName));
- const srcsetValues = await Promise.all(srcset.map(async srcsetValue => {
- const resourceURL = DomUtil.normalizeURL(srcsetValue.url);
- if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
- try {
- const dataURI = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
- return dataURI + (srcsetValue.w ? " " + srcsetValue.w + "w" : srcsetValue.d ? " " + srcsetValue.d + "x" : "");
- } catch (error) {
- /* ignored */
- }
- }
- }));
- resourceElement.setAttribute(attributeName, srcsetValues.join(","));
- }));
- }
- }
- // -------
- // DomUtil
- // -------
- const DATA_URI_PREFIX = "data:";
- const BLOB_URI_PREFIX = "blob:";
- const ABOUT_BLANK_URI = "about:blank";
- const REGEXP_URL_FN = /(url\s*\(\s*'(.*?)'\s*\))|(url\s*\(\s*"(.*?)"\s*\))|(url\s*\(\s*(.*?)\s*\))/gi;
- const REGEXP_URL_SIMPLE_QUOTES_FN = /^url\s*\(\s*'(.*?)'\s*\)$/i;
- const REGEXP_URL_DOUBLE_QUOTES_FN = /^url\s*\(\s*"(.*?)"\s*\)$/i;
- const REGEXP_URL_NO_QUOTES_FN = /^url\s*\(\s*(.*?)\s*\)$/i;
- 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;
- const REGEXP_IMPORT_URL_SIMPLE_QUOTES_FN = /@import\s*url\s*\(\s*'(.*?)'\s*\)\s*(.*?)/i;
- const REGEXP_IMPORT_URL_DOUBLE_QUOTES_FN = /@import\s*url\s*\(\s*"(.*?)"\s*\)\s*(.*?)/i;
- const REGEXP_IMPORT_URL_NO_QUOTES_FN = /@import\s*url\s*\(\s*(.*?)\s*\)\s*(.*?)/i;
- const REGEXP_IMPORT_SIMPLE_QUOTES_FN = /@import\s*'(.*?)'\s*(.*?)/i;
- const REGEXP_IMPORT_DOUBLE_QUOTES_FN = /@import\s*"(.*?)"\s*(.*?)/i;
- const REGEXP_IMPORT_NO_QUOTES_FN = /@import\s*(.*?)\s*(.*?)/i;
- class DomUtil {
- static normalizeURL(url) {
- return url.split("#")[0];
- }
- static getUrlFunctions(stylesheetContent) {
- return stylesheetContent.match(REGEXP_URL_FN) || [];
- }
- static getImportFunctions(stylesheetContent) {
- return stylesheetContent.match(REGEXP_IMPORT_FN) || [];
- }
- static matchURL(stylesheetContent) {
- const match = stylesheetContent.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_URL_NO_QUOTES_FN);
- return match && match[1];
- }
- static testValidPath(resourceURL) {
- return !resourceURL.startsWith(DATA_URI_PREFIX) && !resourceURL.startsWith(BLOB_URI_PREFIX) && resourceURL != ABOUT_BLANK_URI;
- }
- static matchImport(stylesheetContent) {
- const match = stylesheetContent.match(REGEXP_IMPORT_URL_SIMPLE_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_IMPORT_URL_DOUBLE_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_IMPORT_URL_NO_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_IMPORT_SIMPLE_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_IMPORT_DOUBLE_QUOTES_FN) ||
- stylesheetContent.match(REGEXP_IMPORT_NO_QUOTES_FN);
- if (match) {
- const [, resourceURL, media] = match;
- return { resourceURL, media };
- }
- }
- static removeCssComments(stylesheetContent) {
- let start, end;
- do {
- start = stylesheetContent.indexOf("/*");
- end = stylesheetContent.indexOf("*/", start);
- if (start != -1 && end != -1) {
- stylesheetContent = stylesheetContent.substring(0, start) + stylesheetContent.substr(end + 2);
- }
- } while (start != -1 && end != -1);
- return stylesheetContent;
- }
- static wrapMediaQuery(stylesheetContent, mediaQuery) {
- if (mediaQuery) {
- return "@media " + mediaQuery + "{ " + stylesheetContent + " }";
- } else {
- return stylesheetContent;
- }
- }
- }
- return { getClass };
- })();
|