Ver código fonte

split removeDiscardedResources method

Gildas 7 anos atrás
pai
commit
3360159271
1 arquivos alterados com 39 adições e 37 exclusões
  1. 39 37
      lib/single-file/single-file-core.js

+ 39 - 37
lib/single-file/single-file-core.js

@@ -95,6 +95,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			{ option: "removeFrames", action: "removeFrames" },
 			{ option: "removeImports", action: "removeImports" },
 			{ option: "removeScripts", action: "removeScripts" },
+			{ option: "removeVideoSrc", action: "removeVideoSources" },
+			{ option: "removeAudioSrc", action: "removeAudiosources" },
 			{ action: "removeDiscardedResources" },
 			{ action: "resetCharsetMeta" },
 			{ action: "setInputValues" },
@@ -461,43 +463,6 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				this.doc.querySelectorAll("input[type=hidden]").forEach(element => element.remove());
 			}
 			this.doc.querySelectorAll("a[ping]").forEach(element => element.removeAttribute("ping"));
-			if (this.options.removeScripts) {
-				this.onEventAttributeNames.forEach(attributeName => this.doc.querySelectorAll("[" + attributeName + "]").forEach(element => element.removeAttribute(attributeName)));
-				this.doc.querySelectorAll("[href]").forEach(element => {
-					if (element.href && element.href.match && element.href.match(/^\s*javascript:/)) {
-						element.setAttribute("href", "");
-					}
-				});
-				this.doc.querySelectorAll("[src]").forEach(element => {
-					if (element.src && element.src.match(/^\s*javascript:/)) {
-						element.removeAttribute("src");
-					}
-				});
-			}
-			const audioSourceElements = this.doc.querySelectorAll("audio[src], audio > source[src]");
-			this.stats.set("processed", "audio sources", audioSourceElements.length);
-			if (this.options.removeAudioSrc) {
-				this.stats.set("discarded", "audio sources", audioSourceElements.length);
-				audioSourceElements.forEach(element => {
-					if (element.tagName == "SOURCE") {
-						element.remove();
-					} else {
-						audioSourceElements.forEach(element => element.removeAttribute("src"));
-					}
-				});
-			}
-			const videoSourceElements = this.doc.querySelectorAll("video[src], video > source");
-			this.stats.set("processed", "video sources", videoSourceElements.length);
-			if (this.options.removeVideoSrc) {
-				this.stats.set("discarded", "video sources", videoSourceElements.length);
-				videoSourceElements.forEach(element => {
-					if (element.tagName == "SOURCE") {
-						element.remove();
-					} else {
-						videoSourceElements.forEach(element => element.removeAttribute("src"));
-					}
-				});
-			}
 		}
 
 		async end() {
@@ -560,12 +525,49 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		}
 
 		removeScripts() {
+			this.onEventAttributeNames.forEach(attributeName => this.doc.querySelectorAll("[" + attributeName + "]").forEach(element => element.removeAttribute(attributeName)));
+			this.doc.querySelectorAll("[href]").forEach(element => {
+				if (element.href && element.href.match && element.href.match(/^\s*javascript:/)) {
+					element.setAttribute("href", "");
+				}
+			});
+			this.doc.querySelectorAll("[src]").forEach(element => {
+				if (element.src && element.src.match(/^\s*javascript:/)) {
+					element.removeAttribute("src");
+				}
+			});
 			const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"])");
 			this.stats.set("discarded", "scripts", scriptElements.length);
 			this.stats.set("processed", "scripts", scriptElements.length);
 			scriptElements.forEach(element => element.remove());
 		}
 
+		removeAudioSources() {
+			const audioSourceElements = this.doc.querySelectorAll("audio[src], audio > source[src]");
+			this.stats.set("discarded", "audio sources", audioSourceElements.length);
+			this.stats.set("processed", "audio sources", audioSourceElements.length);
+			audioSourceElements.forEach(element => {
+				if (element.tagName == "SOURCE") {
+					element.remove();
+				} else {
+					audioSourceElements.forEach(element => element.removeAttribute("src"));
+				}
+			});
+		}
+
+		removeVideoSources() {
+			const videoSourceElements = this.doc.querySelectorAll("video[src], video > source");
+			this.stats.set("discarded", "video sources", videoSourceElements.length);
+			this.stats.set("processed", "video sources", videoSourceElements.length);
+			videoSourceElements.forEach(element => {
+				if (element.tagName == "SOURCE") {
+					element.remove();
+				} else {
+					videoSourceElements.forEach(element => element.removeAttribute("src"));
+				}
+			});
+		}
+
 		removeFrames() {
 			const frameElements = this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]");
 			this.stats.set("discarded", "frames", frameElements.length);