Pārlūkot izejas kodu

added handling of non-serialized rules

Gildas 7 gadi atpakaļ
vecāks
revīzija
1b68b86ea8

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

@@ -171,6 +171,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 
 	async function getOptions(options) {
 		options.canvasData = getCanvasData();
+		options.emptyStylesRulesText = getEmptyStyleRulesText();
 		if (!options.removeFrames) {
 			options.framesData = await FrameTree.getFramesData();
 		}
@@ -196,6 +197,16 @@ this.singlefile.top = this.singlefile.top || (() => {
 		return options;
 	}
 
+	function getEmptyStyleRulesText() {
+		const textData = [];
+		document.querySelectorAll("style").forEach(styleElement => {
+			if (!styleElement.textContent) {
+				textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
+			}
+		});
+		return textData;
+	}
+
 	function getCanvasData() {
 		const canvasData = [];
 		document.querySelectorAll("canvas").forEach(canvasElement => {

+ 13 - 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.replaceEmptyStyles();
 			if (!this.options.jsEnabled || (this.options.saveRawPage && this.options.removeScripts)) {
 				this.processor.insertNoscriptContents();
 			}
@@ -488,6 +489,18 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			}
 		}
 
+		replaceEmptyStyles() {
+			if (this.options.emptyStylesRulesText) {
+				let indexStyle = 0;
+				this.doc.querySelectorAll("style").forEach(styleElement => {
+					if (!styleElement.textContent) {
+						styleElement.textContent = this.options.emptyStylesRulesText[indexStyle];
+						indexStyle++;
+					}
+				});
+			}
+		}
+
 		async pageResources() {
 			const resourcePromises = [
 				DomProcessorHelper.processAttribute(this.doc.querySelectorAll("link[href][rel*=\"icon\"]"), "href", this.baseURI),