Răsfoiți Sursa

added singlefile.lib.initialize method

Former-commit-id: 5c71162023dfbadc911a1d09f42a8755bd19abf1
Gildas 6 ani în urmă
părinte
comite
90e065d4b4

+ 1 - 5
cli/back-ends/jsdom.js

@@ -82,12 +82,8 @@ exports.getPageData = async options => {
 			await new Promise(resolve => win.document.onload = resolve);
 		}
 		executeFrameScripts(doc, scripts);
-		if (!options.saveRawPage && !options.removeFrames) {
-			options.frames = await win.singlefile.lib.frameTree.content.frames.getAsync(options);
-		}
-		options.win = win;
-		options.doc = doc;
 		options.removeHiddenElements = false;
+		await win.singlefile.lib.initialize(options, doc, win);
 		const pageData = await win.singlefile.lib.getPageData(options, { fetch: url => fetchResource(url, options) });
 		if (options.includeInfobar) {
 			await win.singlefile.common.ui.content.infobar.includeScript(pageData);

+ 1 - 13
cli/back-ends/puppeteer.js

@@ -71,19 +71,7 @@ exports.getPageData = async options => {
 		});
 		try {
 			return await page.evaluate(async options => {
-				singlefile.lib.helper.initDoc(document);
-				const preInitializationPromises = [];
-				if (!options.saveRawPage) {
-					if (!options.removeFrames) {
-						preInitializationPromises.push(singlefile.lib.frameTree.content.frames.getAsync(options));
-					}
-					if (options.loadDeferredImages) {
-						preInitializationPromises.push(singlefile.lib.lazy.content.loader.process(options));
-					}
-				}
-				[options.frames] = await Promise.all(preInitializationPromises);
-				options.doc = document;
-				options.win = window;
+				await singlefile.lib.initialize(options, document, window);
 				const pageData = await singlefile.lib.getPageData(options);
 				if (options.includeInfobar) {
 					await singlefile.common.ui.content.infobar.includeScript(pageData);

+ 1 - 13
cli/back-ends/webdriver-chromium.js

@@ -137,19 +137,7 @@ function getPageDataScript() {
 		.catch(error => callback({ error: error.toString() }));
 
 	async function getPageData() {
-		singlefile.lib.helper.initDoc(document);
-		const preInitializationPromises = [];
-		if (!options.saveRawPage) {
-			if (!options.removeFrames) {
-				preInitializationPromises.push(singlefile.lib.frameTree.content.frames.getAsync(options));
-			}
-			if (options.loadDeferredImages) {
-				preInitializationPromises.push(singlefile.lib.lazy.content.loader.process(options));
-			}
-		}
-		[options.frames] = await Promise.all(preInitializationPromises);
-		options.doc = document;
-		options.win = window;		
+		await singlefile.lib.initialize(options, document, window);
 		const pageData = await singlefile.lib.getPageData(options);
 		if (options.includeInfobar) {
 			await singlefile.common.ui.content.infobar.includeScript(pageData);

+ 1 - 13
cli/back-ends/webdriver-gecko.js

@@ -144,19 +144,7 @@ function getPageDataScript() {
 		.catch(error => callback({ error: error.toString() }));
 
 	async function getPageData() {
-		singlefile.lib.helper.initDoc(document);
-		const preInitializationPromises = [];
-		if (!options.saveRawPage) {
-			if (!options.removeFrames) {
-				preInitializationPromises.push(singlefile.lib.frameTree.content.frames.getAsync(options));
-			}
-			if (options.loadDeferredImages) {
-				preInitializationPromises.push(singlefile.lib.lazy.content.loader.process(options));
-			}
-		}
-		[options.frames] = await Promise.all(preInitializationPromises);
-		options.doc = document;
-		options.win = window;		
+		await singlefile.lib.initialize(options, document, window);
 		const pageData = await singlefile.lib.getPageData(options);
 		if (options.includeInfobar) {
 			await singlefile.common.ui.content.infobar.includeScript(pageData);

+ 15 - 0
lib/index.js

@@ -34,6 +34,21 @@ this.singlefile = this.singlefile || {
 		},
 		vendor: {},
 		modules: {},
+		initialize: async function (options = {}, doc, win) {
+			this.helper.initDoc(doc);
+			const preInitializationPromises = [];
+			if (!options.saveRawPage) {
+				if (!options.removeFrames) {
+					preInitializationPromises.push(this.frameTree.content.frames.getAsync(options));
+				}
+				if (options.loadDeferredImages) {
+					preInitializationPromises.push(this.lazy.content.loader.process(options));
+				}
+			}
+			[options.frames] = await Promise.all(preInitializationPromises);
+			options.doc = doc;
+			options.win = win;
+		},
 		getPageData: async function (options = {}, classOptions) {
 			options.insertSingleFileComment = true;
 			options.insertFaviconLink = true;