瀏覽代碼

split processFrames into 2 functions

Gildas 7 年之前
父節點
當前提交
b6a4e6f9ad
共有 1 個文件被更改,包括 30 次插入27 次删除
  1. 30 27
      lib/single-file/frame-tree.js

+ 30 - 27
lib/single-file/frame-tree.js

@@ -27,12 +27,13 @@ this.frameTree = this.frameTree || (() => {
 	const INIT_REQUEST_MESSAGE = "initRequest";
 	const INIT_RESPONSE_MESSAGE = "initResponse";
 	const TIMEOUT_INIT_REQUEST_MESSAGE = 500;
+	const TOP_WINDOW_ID = "0";
 	const TOP_WINDOW = isTopWindow(window);
 
 	let sessions = new Map(), windowId;
 
 	if (TOP_WINDOW) {
-		windowId = "0";
+		windowId = TOP_WINDOW_ID;
 	}
 	addEventListener("message", event => {
 		if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
@@ -115,6 +116,13 @@ this.frameTree = this.frameTree || (() => {
 	}
 
 	function processFrames(frameElements, options, parentWindowId, sessionId) {
+		processFramesSync(frameElements, options, parentWindowId, sessionId);
+		if (frameElements.length) {
+			processFramesAsync(frameElements, options, parentWindowId, sessionId);
+		}
+	}
+
+	function processFramesSync(frameElements, options, parentWindowId, sessionId) {
 		let framesData = [];
 		frameElements.forEach((frameElement, frameIndex) => {
 			const windowId = parentWindowId + "." + frameIndex;
@@ -130,33 +138,28 @@ this.frameTree = this.frameTree || (() => {
 			timeout.set(() => sendInitResponse({ framesData: [{ windowId, processed: true, timeout: true }], windowId, sessionId }, "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
 		});
 		sendInitResponse({ framesData, parentWindowId, sessionId });
-		if (frameElements.length) {
-			framesData = [];
-			frameElements.forEach((frameElement, frameIndex) => {
-				const windowId = parentWindowId + "." + frameIndex;
-				const frameWindow = frameElement.contentWindow;
-				const frameDoc = frameElement.contentDocument;
-				if (frameDoc) {
-					try {
-						processFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, windowId, sessionId);
-						const docData = docHelper.preProcessDoc(frameDoc, frameWindow, options);
-						framesData.push({
-							windowId,
-							content: docHelper.serialize(frameDoc),
-							baseURI: frameDoc.baseURI.split("#")[0],
-							title: frameDoc.title,
-							stylesheetContents: docData.stylesheetContents,
-							canvasData: docData.canvasData,
-							processed: true
-						});
-						docHelper.postProcessDoc(frameDoc, frameWindow, options);
-					} catch (error) {
-						framesData.push({ windowId, processed: true });
-					}
+	}
+
+	function processFramesAsync(frameElements, options, parentWindowId, sessionId) {
+		let framesData = [];
+		frameElements.forEach((frameElement, frameIndex) => {
+			const windowId = parentWindowId + "." + frameIndex;
+			const frameWindow = frameElement.contentWindow;
+			const frameDoc = frameElement.contentDocument;
+			if (frameDoc) {
+				try {
+					processFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, windowId, sessionId);
+					const docData = docHelper.preProcessDoc(frameDoc, frameWindow, options);
+					const content = docHelper.serialize(frameDoc);
+					const baseURI = frameDoc.baseURI.split("#")[0];
+					framesData.push({ windowId, content, baseURI, title: frameDoc.title, stylesheetContents: docData.stylesheetContents, canvasData: docData.canvasData, processed: true });
+					docHelper.postProcessDoc(frameDoc, frameWindow, options);
+				} catch (error) {
+					framesData.push({ windowId, processed: true });
 				}
-			});
-			sendInitResponse({ framesData, parentWindowId, sessionId });
-		}
+			}
+		});
+		sendInitResponse({ framesData, parentWindowId, sessionId });
 	}
 
 	function sendInitResponse(message) {