Bläddra i källkod

disable noscript tags before getting page content

Gildas 7 år sedan
förälder
incheckning
87ea9b6603
2 ändrade filer med 28 tillägg och 0 borttagningar
  1. 19 0
      extension/core/content/content.js
  2. 9 0
      lib/single-file/single-file-core.js

+ 19 - 0
extension/core/content/content.js

@@ -54,6 +54,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 		options = await getOptions(options);
 		const processor = new (SingleFile.getClass())(options);
 		fixInlineScripts();
+		disableNoscriptTags();
 		if (options.selected) {
 			markSelectedContent(processor.SELECTED_CONTENT_ATTRIBUTE_NAME, processor.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME);
 		}
@@ -69,6 +70,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 		if (options.shadowEnabled) {
 			singlefile.ui.init();
 		}
+		enableDisabledNoscriptTags();
 		if (options.removeHiddenElements) {
 			unmarkRemovedElements(processor.REMOVED_CONTENT_ATTRIBUTE_NAME);
 		}
@@ -100,6 +102,23 @@ this.singlefile.top = this.singlefile.top || (() => {
 		URL.revokeObjectURL(page.url);
 	}
 
+	function disableNoscriptTags() {
+		document.head.querySelectorAll("noscript").forEach(element => {
+			const disabledNoscriptElement = document.createElement("disabled-noscript");
+			Array.from(element.childNodes).forEach(node => disabledNoscriptElement.appendChild(node));
+			disabledNoscriptElement.hidden = true;
+			element.parentElement.replaceChild(disabledNoscriptElement, element);
+		});
+	}
+
+	function enableDisabledNoscriptTags() {
+		document.head.querySelectorAll("disabled-noscript").forEach(element => {
+			const noscriptElement = document.createElement("noscript");
+			Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
+			element.parentElement.replaceChild(noscriptElement, element);
+		});
+	}
+
 	function fixInlineScripts() {
 		document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
 	}

+ 9 - 0
lib/single-file/single-file-core.js

@@ -90,6 +90,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		async initialize() {
 			this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
 			this.processor.removeInfoToolbar();
+			this.processor.enableDisabledNoscriptTags();
 			this.processor.replaceEmptyStyles();
 			if (!this.options.jsEnabled || (this.options.saveRawPage && this.options.removeScripts)) {
 				this.processor.insertNoscriptContents();
@@ -462,6 +463,14 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
 		}
 
+		enableDisabledNoscriptTags() {
+			this.doc.querySelectorAll("disabled-noscript").forEach(element => {
+				const noscriptElement = this.doc.createElement("noscript");
+				Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
+				element.parentElement.replaceChild(noscriptElement, element);
+			});
+		}
+
 		replaceCanvasElements() {
 			if (this.options.canvasData) {
 				this.doc.querySelectorAll("canvas").forEach((canvasElement, indexCanvasElement) => {