Преглед изворни кода

refactored SingleFileCore to an instance class

Gildas пре 7 година
родитељ
комит
3036d77632
2 измењених фајлова са 56 додато и 46 уклоњено
  1. 36 35
      extension/core/content/content.js
  2. 20 11
      lib/single-file/single-file-core.js

+ 36 - 35
extension/core/content/content.js

@@ -36,43 +36,9 @@
 			processing = true;
 			fixInlineScripts();
 			fixHeadNoScripts();
-			getOptions(message.options)
-				.then(options => {
-					if (options.selected) {
-						selectSelectedContent();
-					}
-					if (!options.removeFrames) {
-						hideHeadFrames();
-					}
-					if (options.removeHiddenElements) {
-						selectRemovedElements();
-					}
-					options.url = document.location.href;
-					options.content = getDoctype(document) + document.documentElement.outerHTML;
-					if (options.removeHiddenElements) {
-						unselectRemovedElements();
-					}
-					if (options.selected) {
-						unselectSelectedContent();
-					}
-					return SingleFile.initialize(options);
-				})
-				.then(process => {
-					const options = message.options;
-					if (options.shadowEnabled) {
-						singlefile.ui.init();
-					}
-					return process();
-				})
+			processMessage(message)
 				.then(page => {
-					const options = message.options;
-					const date = new Date();
-					page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
-					page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
 					downloadPage(page);
-					if (options.shadowEnabled) {
-						singlefile.ui.end();
-					}
 					processing = false;
 				})
 				.catch(error => {
@@ -83,6 +49,41 @@
 		}
 	});
 
+	async function processMessage(message) {
+		const options = await getOptions(message.options);
+		if (options.selected) {
+			selectSelectedContent();
+		}
+		if (!options.removeFrames) {
+			hideHeadFrames();
+		}
+		if (options.removeHiddenElements) {
+			selectRemovedElements();
+		}
+		options.url = document.location.href;
+		options.content = getDoctype(document) + document.documentElement.outerHTML;
+		if (options.removeHiddenElements) {
+			unselectRemovedElements();
+		}
+		if (options.selected) {
+			unselectSelectedContent();
+		}
+		const processor = new SingleFile(options);
+		await processor.initialize();
+		if (options.shadowEnabled) {
+			singlefile.ui.init();
+		}
+		await processor.preparePageData();
+		const page = processor.getPageData();
+		const date = new Date();
+		page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
+		page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" })); // TODO: revoke after download
+		if (options.shadowEnabled) {
+			singlefile.ui.end();
+		}
+		return page;
+	}
+
 	function fixInlineScripts() {
 		document.querySelectorAll("script").forEach(element => element.textContent = element.textContent.replace(/<\/script>/gi, "<\\/script>"));
 	}

+ 20 - 11
lib/single-file/single-file-core.js

@@ -28,14 +28,19 @@ const SingleFileCore = (() => {
 	function SingleFileCore(...args) {
 		[Download, DOM, URL] = args;
 		return class {
-			static async initialize(options) {
-				const processor = new PageProcessor(options);
-				processor.onprogress = options.onprogress;
-				await processor.loadPage(options.content);
-				return async () => {
-					await processor.initialize();
-					return await processor.getPageData();
-				};
+			constructor(options) {
+				this.options = options;
+			}
+			async initialize() {
+				this.processor = new PageProcessor(this.options);
+				await this.processor.loadPage();
+				await this.processor.initialize();
+			}
+			async preparePageData() {
+				await this.processor.preparePageData();
+			}
+			getPageData() {
+				return this.processor.getPageData();
 			}
 		};
 	}
@@ -64,13 +69,14 @@ const SingleFileCore = (() => {
 		constructor(options) {
 			this.options = options;
 			this.processor = new DOMProcessor(options);
+			this.onprogress = options.onprogress;
 		}
 
-		async loadPage(pageContent) {
+		async loadPage() {
 			if (this.onprogress) {
 				this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
 			}
-			await this.processor.loadPage(pageContent);
+			await this.processor.loadPage(this.options.content);
 			if (this.onprogress) {
 				this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
 			}
@@ -128,7 +134,7 @@ const SingleFileCore = (() => {
 			}
 		}
 
-		async getPageData() {
+		async preparePageData() {
 			await this.processor.retrieveResources(
 				details => {
 					if (this.onprogress) {
@@ -155,6 +161,9 @@ const SingleFileCore = (() => {
 			if (!this.options.removeImports) {
 				await this.processor.htmlImports();
 			}
+		}
+
+		getPageData() {
 			if (this.onprogress) {
 				this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
 			}