ソースを参照

use constants for event types

Gildas 7 年 前
コミット
3527815272

+ 3 - 3
extension/core/scripts/content/client.js

@@ -30,21 +30,21 @@
 			options.content = getDoctype(document) + document.documentElement.outerHTML;
 			options.jsEnabled = true;
 			options.onprogress = event => {
-				if (event.type == "resources-initialized") {
+				if (event.type == event.RESOURCES_INITIALIZED) {
 					chrome.runtime.sendMessage({
 						processStart: true,
 						index: event.details.index,
 						maxIndex: event.details.max
 					});
 				}
-				if (event.type == "resource-loaded") {
+				if (event.type == event.RESOURCE_LOADED) {
 					chrome.runtime.sendMessage({
 						processProgress: true,
 						index: event.details.index,
 						maxIndex: event.details.max
 					});
 				}
-				if (event.type == "page-ended") {
+				if (event.type == event.PAGE_ENDED) {
 					chrome.runtime.sendMessage({ processEnd: true });
 				}
 			};

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

@@ -35,6 +35,23 @@ const SingleFileCore = (() => {
 		};
 	}
 
+	// -------------
+	// ProgressEvent
+	// -------------
+	const PAGE_LOADING = "page-loading";
+	const PAGE_LOADED = "page-loaded";
+	const RESOURCES_INITIALIZING = "resource-initializing";
+	const RESOURCES_INITIALIZED = "resources-initialized";
+	const RESOURCE_LOADING = "resource-loading";
+	const RESOURCE_LOADED = "resource-loaded";
+	const PAGE_ENDED = "page-ended";
+
+	class ProgressEvent {
+		constructor(type, details) {
+			return { type, details, PAGE_LOADING, PAGE_LOADED, RESOURCES_INITIALIZING, RESOURCES_INITIALIZED, RESOURCE_LOADING, RESOURCE_LOADED, PAGE_ENDED };
+		}
+	}
+
 	// -------------
 	// PageProcessor
 	// -------------
@@ -46,17 +63,17 @@ const SingleFileCore = (() => {
 
 		async loadPage(pageContent) {
 			if (this.onprogress) {
-				this.onprogress({ type: "page-loading", details: { pageURL: this.options.url } });
+				this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
 			}
 			await this.processor.loadPage(pageContent);
 			if (this.onprogress) {
-				this.onprogress({ type: "page-loaded", details: { pageURL: this.options.url } });
+				this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
 			}
 		}
 
 		async initialize() {
 			if (this.onprogress) {
-				this.onprogress({ type: "resources-initializing", details: { pageURL: this.options.url } });
+				this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
 			}
 			if (!this.options.jsEnabled) {
 				this.processor.insertNoscriptContents();
@@ -75,7 +92,7 @@ const SingleFileCore = (() => {
 			await Promise.all([this.processor.inlineStylesheets(true), this.processor.linkStylesheets()], this.processor.attributeStyles(true));
 			this.pendingPromises = Promise.all([this.processor.inlineStylesheets(), this.processor.attributeStyles(), this.processor.pageResources()]);
 			if (this.onprogress) {
-				this.onprogress({ type: "resources-initialized", details: { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() } });
+				this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() }));
 			}
 		}
 
@@ -84,13 +101,13 @@ const SingleFileCore = (() => {
 				details => {
 					if (this.onprogress) {
 						details.pageURL = this.options.url;
-						this.onprogress({ type: "resource-loading", details });
+						this.onprogress(new ProgressEvent(RESOURCE_LOADING, details));
 					}
 				},
 				details => {
 					if (this.onprogress) {
 						details.pageURL = this.options.url;
-						this.onprogress({ type: "resource-loaded", details });
+						this.onprogress(new ProgressEvent(RESOURCE_LOADED, details));
 					}
 				});
 			await this.pendingPromises;
@@ -98,7 +115,7 @@ const SingleFileCore = (() => {
 				this.processor.removeUnusedCSSRules();
 			}
 			if (this.onprogress) {
-				this.onprogress({ type: "page-ended", details: { pageURL: this.options.url } });
+				this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
 			}
 			return this.processor.getContent();
 		}