|
|
@@ -78,7 +78,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
|
|
|
// -------------
|
|
|
const STAGES = [{
|
|
|
sequential: [
|
|
|
- { action: "repairDocument" },
|
|
|
+ { action: "preProcessPage" },
|
|
|
{ action: "replaceStyleContents" },
|
|
|
{ option: "selected", action: "removeUnselectedElements" },
|
|
|
{ option: "removeVideoSrc", action: "insertVideoPosters" },
|
|
|
@@ -116,6 +116,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
|
|
|
}, {
|
|
|
sequential: [
|
|
|
{ option: "lazyLoadImages", action: "lazyLoadImages" },
|
|
|
+ { option: "removeAlternativeImages", action: "removeAlternativeImages" },
|
|
|
{ option: "removeAlternativeFonts", action: "removeAlternativeFonts" },
|
|
|
{ option: "compressCSS", action: "compressCSS" }
|
|
|
],
|
|
|
@@ -436,9 +437,18 @@ this.SingleFileCore = this.SingleFileCore || (() => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- repairDocument() {
|
|
|
+ preProcessPage() {
|
|
|
this.doc.querySelectorAll("singlefile-infobar, singlefile-mask").forEach(element => element.remove());
|
|
|
this.doc.body.querySelectorAll("title, meta").forEach(element => this.doc.head.appendChild(element));
|
|
|
+ if (this.options.imageData) {
|
|
|
+ const dataAttributeName = DOM.imagesAttributeName(this.options.sessionId);
|
|
|
+ this.doc.querySelectorAll("img").forEach(imgElement => {
|
|
|
+ const imgData = this.options.imageData[Number(imgElement.getAttribute(dataAttributeName))];
|
|
|
+ if (imgData.src) {
|
|
|
+ imgElement.setAttribute("src", imgData.src);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
removeScripts() {
|
|
|
@@ -522,56 +532,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
|
|
|
}
|
|
|
|
|
|
removeAlternativeImages() {
|
|
|
- this.doc.querySelectorAll("picture, img[srcset]").forEach(element => {
|
|
|
- const tagName = element.tagName.toLowerCase();
|
|
|
- const dataAttributeName = DOM.responsiveImagesAttributeName(this.options.sessionId);
|
|
|
- const responsiveImageData = this.options.responsiveImageData && this.options.responsiveImageData[Number(element.getAttribute(dataAttributeName))];
|
|
|
- element.removeAttribute(dataAttributeName);
|
|
|
- if (responsiveImageData) {
|
|
|
- if (tagName == "img") {
|
|
|
- if (responsiveImageData.source.src && responsiveImageData.source.naturalWidth > 1 && responsiveImageData.source.naturalHeight > 1) {
|
|
|
- element.setAttribute("srcset", "");
|
|
|
- element.setAttribute("sizes", "");
|
|
|
- element.src = responsiveImageData.source.src;
|
|
|
- }
|
|
|
- }
|
|
|
- if (tagName == "picture") {
|
|
|
- const imageElement = element.querySelector("img");
|
|
|
- if (responsiveImageData.source && responsiveImageData.source.src && responsiveImageData.source.naturalWidth > 1 && responsiveImageData.source.naturalHeight > 1) {
|
|
|
- imageElement.setAttribute("srcset", "");
|
|
|
- imageElement.setAttribute("sizes", "");
|
|
|
- imageElement.src = responsiveImageData.source.src;
|
|
|
- element.querySelectorAll("source").forEach(sourceElement => sourceElement.remove());
|
|
|
- } else {
|
|
|
- if (responsiveImageData.sources) {
|
|
|
- element.querySelectorAll("source").forEach(sourceElement => {
|
|
|
- if (!sourceElement.srcset && !sourceElement.dataset.srcset && !sourceElement.src) {
|
|
|
- sourceElement.remove();
|
|
|
- }
|
|
|
- });
|
|
|
- const sourceElements = element.querySelectorAll("source");
|
|
|
- if (sourceElements.length) {
|
|
|
- const lastSourceElement = sourceElements[sourceElements.length - 1];
|
|
|
- if (imageElement) {
|
|
|
- if (lastSourceElement.src) {
|
|
|
- imageElement.src = lastSourceElement.src;
|
|
|
- } else {
|
|
|
- imageElement.removeAttribute("src");
|
|
|
- }
|
|
|
- if (lastSourceElement.srcset || lastSourceElement.dataset.srcset) {
|
|
|
- imageElement.srcset = lastSourceElement.srcset || lastSourceElement.dataset.srcset;
|
|
|
- } else {
|
|
|
- imageElement.removeAttribute("srcset");
|
|
|
- }
|
|
|
- element.querySelectorAll("source").forEach(sourceElement => sourceElement.remove());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ DOM.removeAlternativeImages(this.doc, this.options);
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
|
|
|
postRemoveAlternativeFonts() {
|
|
|
DOM.minifyFonts(this.doc, true);
|
|
|
@@ -1351,7 +1313,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
|
|
|
const dataAttributeName = DOM.imagesAttributeName(options.sessionId);
|
|
|
if (imgElement.getAttribute(dataAttributeName) != null) {
|
|
|
const imgData = options.imageData[Number(imgElement.getAttribute(dataAttributeName))];
|
|
|
- imgElement.setAttribute("src", `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="${imgData.pxWidth}" height="${imgData.pxHeight}"><rect fill-opacity="0"/></svg>`);
|
|
|
+ if (imgData.replaceable) {
|
|
|
+ imgElement.setAttribute("src", `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="${imgData.size.pxWidth}" height="${imgData.size.pxHeight}"><rect fill-opacity="0"/></svg>`);
|
|
|
if (options.lazyLoadImages) {
|
|
|
imgElement.setAttribute(GROUPED_IMG_ATTRIBUTE_NAME, "");
|
|
|
}
|
|
|
@@ -1368,6 +1331,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|