/* * Copyright 2018 Gildas Lormeau * contact : gildas.lormeau 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 . */ /* global SingleFileCore, base64, DOMParser, TextDecoder, fetch, superFetch, parseSrcset, uglifycss, htmlmini, rulesMinifier, lazyLoader, serializer */ this.SingleFile = this.SingleFile || (() => { const ONE_MB = 1024 * 1024; // -------- // Download // -------- let fetchResource; class Download { static async getContent(resourceURL, options) { let resourceContent; if (!fetchResource) { fetchResource = typeof superFetch == "undefined" ? fetch : superFetch.fetch; } try { resourceContent = await fetchResource(resourceURL); } catch (error) { return options && options.asDataURI ? "data:base64," : ""; } let contentType = resourceContent.headers.get("content-type"); if (contentType) { contentType = contentType.match(/^([^;]*)/)[0]; } if (options && options.asDataURI) { try { const buffer = await resourceContent.arrayBuffer(); const dataURI = "data:" + (contentType || "") + ";" + "base64," + base64.fromByteArray(new Uint8Array(buffer)); if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) { return "data:base64,"; } else { return dataURI; } } catch (error) { return "data:base64,"; } } else { const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i); let charSet; if (matchCharset && matchCharset[1]) { charSet = matchCharset[1].toLowerCase(); } if (!charSet) { charSet = "utf-8"; } try { const arrayBuffer = await resourceContent.arrayBuffer(); const textContent = (new TextDecoder(charSet)).decode(arrayBuffer); if (options.maxResourceSizeEnabled && textContent.length > options.maxResourceSize * ONE_MB) { return ""; } else { return textContent; } } catch (error) { return ""; } } } } // --- // DOM // --- class DOM { static create(pageContent, baseURI) { const doc = (new DOMParser()).parseFromString(pageContent, "text/html"); let baseElement = doc.querySelector("base"); if (!baseElement || !baseElement.getAttribute("href")) { if (baseElement) { baseElement.remove(); } baseElement = doc.createElement("base"); baseElement.setAttribute("href", baseURI); doc.head.insertBefore(baseElement, doc.head.firstChild); } return { DOMParser, document: doc, serialize: options => serializer.process(doc, options.compressHTML), parseSrcset: srcset => parseSrcset.process(srcset), uglifycss: (content, options) => uglifycss.processString(content, options), lazyLoader: { process: doc => lazyLoader.process(doc), imageSelectors: lazyLoader.imageSelectors }, htmlmini: { process: (doc, options) => htmlmini.process(doc, options), postProcess: doc => htmlmini.postProcess(doc), }, rulesMinifier: doc => rulesMinifier.process(doc) }; } } return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) }; })();