Ver código fonte

inject preloaded scripts

Former-commit-id: 4f461dbff3cc81f348395100c36de1753daf9f38
Gildas 6 anos atrás
pai
commit
9a5713daa6
1 arquivos alterados com 25 adições e 7 exclusões
  1. 25 7
      lib/single-file/single-file-core.js

+ 25 - 7
lib/single-file/single-file-core.js

@@ -666,7 +666,13 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			objectElements.forEach(element => element.remove());
 			const replacedAttributeValue = this.doc.querySelectorAll("link[rel~=preconnect], link[rel~=prerender], link[rel~=dns-prefetch], link[rel~=preload], link[rel~=prefetch]");
 			replacedAttributeValue.forEach(element => {
-				const relValue = element.getAttribute("rel").replace(/(preconnect|prerender|dns-prefetch|preload|prefetch)/g, "").trim();
+				let regExp;
+				if (this.options.removeScripts) {
+					regExp = /(preconnect|prerender|dns-prefetch|preload|prefetch)/g;
+				} else {
+					regExp = /(preconnect|prerender|dns-prefetch|prefetch)/g;
+				}
+				const relValue = element.getAttribute("rel").replace(regExp, "").trim();
 				if (relValue.length) {
 					element.setAttribute("rel", relValue);
 				} else {
@@ -1112,12 +1118,18 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		}
 
 		async processScripts() {
-			await Promise.all(Array.from(this.doc.querySelectorAll("script[src]")).map(async scriptElement => {
+			await Promise.all(Array.from(this.doc.querySelectorAll("script[src], link[rel=preload][as=script][href]")).map(async element => {
 				let resourceURL;
-				const scriptSrc = scriptElement.getAttribute("src");
-				scriptElement.removeAttribute("src");
-				scriptElement.removeAttribute("integrity");
-				scriptElement.textContent = "";
+				let scriptSrc;
+				if (element.tagName == "SCRIPT") {
+					scriptSrc = element.getAttribute("src");
+					element.removeAttribute("src");
+				} else {
+					scriptSrc = element.getAttribute("href");
+					element.removeAttribute("href");
+				}
+				element.removeAttribute("integrity");
+				element.textContent = "";
 				try {
 					resourceURL = util.resolveURL(scriptSrc, this.baseURI);
 				} catch (error) {
@@ -1132,7 +1144,13 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						frameId: this.options.windowId
 					});
 					content.data = Util.getUpdatedResourceContent(resourceURL, content, this.options);
-					scriptElement.setAttribute("src", content.data);
+					if (element.tagName == "SCRIPT") {
+						element.setAttribute("src", content.data);
+					} else {
+						const scriptElement = this.doc.createElement("script");
+						scriptElement.setAttribute("src", content.data);
+						element.parentElement.replaceChild(scriptElement, element);
+					}
 				}
 			}));
 		}