| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /*
- * 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 SingleFileCore, DOMParser, TextDecoder, Blob, fetch, base64, superFetch, parseSrcset, uglifycss, htmlmini, rulesMinifier, fontsMinifier, lazyLoader, serializer, docHelper */
- 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," : "";
- }
- if (resourceContent.status >= 400) {
- resourceContent = options && options.asDataURI ? "data:base64," : "";
- }
- let contentType = resourceContent.headers && 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 createDoc(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 doc;
- }
- static getParser() {
- return DOMParser;
- }
- static getContentSize(content) {
- return new Blob([content]).size;
- }
- static htmlminiProcess(doc, options) {
- return htmlmini.process(doc, options);
- }
- static htmlminiPostProcess(doc) {
- return htmlmini.postProcess(doc);
- }
- static lazyLoader(doc) {
- return lazyLoader.process(doc);
- }
- static lazyLoaderImageSelectors() {
- return lazyLoader.imageSelectors;
- }
- static rulesMinifier(doc) {
- return rulesMinifier.process(doc);
- }
- static fontsMinifier(doc, secondPass) {
- return fontsMinifier.process(doc, secondPass);
- }
- static uglifycss(content, options) {
- return uglifycss.processString(content, options);
- }
- static parseSrcset(srcset) {
- return parseSrcset.process(srcset);
- }
- static serialize(doc, compressHTML) {
- return serializer.process(doc, compressHTML);
- }
- static preProcessDoc(doc, win, options) {
- return docHelper.preProcessDoc(doc, win, options);
- }
- static postProcessDoc(doc, options) {
- docHelper.postProcessDoc(doc, options);
- }
- static windowIdAttributeName(sessionId) {
- return docHelper.windowIdAttributeName(sessionId);
- }
- static preservedSpaceAttributeName(sessionId) {
- return docHelper.preservedSpaceAttributeName(sessionId);
- }
- static removedContentAttributeName(sessionId) {
- return docHelper.removedContentAttributeName(sessionId);
- }
- }
- return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };
- })();
|