Kaynağa Gözat

Update getPageData to properly end deferred image loading

Gildas 5 yıl önce
ebeveyn
işleme
85dcab2675
1 değiştirilmiş dosya ile 23 ekleme ve 6 silme
  1. 23 6
      lib/single-file/index.js

+ 23 - 6
lib/single-file/index.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global window */
+/* global window, setTimeout */
 
 this.singlefile = this.singlefile || {
 	lib: {
@@ -39,13 +39,20 @@ this.singlefile = this.singlefile || {
 		vendor: {},
 		modules: {},
 		async getPageData(options = {}, initOptions, doc = window.document, win = window) {
+			const frames = this.processors.frameTree.content.frames;
 			this.main.init(initOptions);
 			if (doc && window) {
 				this.helper.initDoc(doc);
 				const preInitializationPromises = [];
 				if (!options.saveRawPage) {
-					if (!options.removeFrames) {
-						preInitializationPromises.push(this.processors.frameTree.content.frames.getAsync(options));
+					if (!options.removeFrames && frames && window.frames && window.frames.length) {
+						let frameTreePromise;
+						if (options.loadDeferredImages) {
+							frameTreePromise = new Promise(resolve => setTimeout(() => resolve(frames.getAsync(options)), options.loadDeferredImagesMaxIdleTime - frames.TIMEOUT_INIT_REQUEST_MESSAGE));
+						} else {
+							frameTreePromise = frames.getAsync(options);
+						}
+						preInitializationPromises.push(frameTreePromise);
 					}
 					if (options.loadDeferredImages) {
 						preInitializationPromises.push(this.processors.lazy.content.loader.process(options));
@@ -57,9 +64,19 @@ this.singlefile = this.singlefile || {
 			options.win = win;
 			options.insertSingleFileComment = true;
 			options.insertCanonicalLink = true;
-			const singleFile = new this.SingleFile(options);
-			await singleFile.run();
-			return await singleFile.getPageData();
+			options.onprogress = event => {
+				if (event.type === event.RESOURCES_INITIALIZED) {
+					if (options.loadDeferredImagesKeepZoomLevel) {
+						this.processors.lazy.content.loader.resetZoomLevel();
+					}
+				}
+			};
+			const processor = new this.SingleFile(options);
+			await processor.run();
+			if (!options.saveRawPage && !options.removeFrames && frames) {
+				frames.cleanup(options);
+			}
+			return await processor.getPageData();
 		}
 	}
 };