Przeglądaj źródła

assign a default onprogress function if not provided

Gildas 7 lat temu
rodzic
commit
18de044bd5
1 zmienionych plików z 10 dodań i 24 usunięć
  1. 10 24
      lib/single-file/single-file-core.js

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

@@ -71,23 +71,17 @@ this.SingleFileCore = (() => {
 		constructor(options) {
 			this.options = options;
 			this.processor = new DOMProcessor(options);
-			this.onprogress = options.onprogress;
+			this.onprogress = options.onprogress || (() => { });
 		}
 
 		async loadPage() {
-			if (this.onprogress) {
-				this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
-			}
+			this.onprogress(new ProgressEvent(PAGE_LOADING, { pageURL: this.options.url }));
 			await this.processor.loadPage(this.options.content);
-			if (this.onprogress) {
-				this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
-			}
+			this.onprogress(new ProgressEvent(PAGE_LOADED, { pageURL: this.options.url }));
 		}
 
 		async initialize() {
-			if (this.onprogress) {
-				this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
-			}
+			this.onprogress(new ProgressEvent(RESOURCES_INITIALIZING, { pageURL: this.options.url }));
 			if (!this.options.jsEnabled || (this.options.saveRawPage && this.options.removeScripts)) {
 				this.processor.insertNoscriptContents();
 			}
@@ -131,24 +125,18 @@ this.SingleFileCore = (() => {
 			if (!this.options.removeScripts) {
 				this.pendingPromises.push(this.processor.scripts());
 			}
-			if (this.onprogress) {
-				this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() }));
-			}
+			this.onprogress(new ProgressEvent(RESOURCES_INITIALIZED, { pageURL: this.options.url, index: 0, max: batchRequest.getMaxResources() }));
 		}
 
 		async preparePageData() {
 			await this.processor.retrieveResources(
 				details => {
-					if (this.onprogress) {
-						details.pageURL = this.options.url;
-						this.onprogress(new ProgressEvent(RESOURCE_LOADING, details));
-					}
+					details.pageURL = this.options.url;
+					this.onprogress(new ProgressEvent(RESOURCE_LOADING, details));
 				},
 				details => {
-					if (this.onprogress) {
-						details.pageURL = this.options.url;
-						this.onprogress(new ProgressEvent(RESOURCE_LOADED, details));
-					}
+					details.pageURL = this.options.url;
+					this.onprogress(new ProgressEvent(RESOURCE_LOADED, details));
 				});
 			await this.pendingPromises;
 			if (this.options.lazyLoadImages) {
@@ -163,9 +151,7 @@ this.SingleFileCore = (() => {
 		}
 
 		getPageData() {
-			if (this.onprogress) {
-				this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
-			}
+			this.onprogress(new ProgressEvent(PAGE_ENDED, { pageURL: this.options.url }));
 			return this.processor.getPageData();
 		}
 	}