فهرست منبع

refactored code

Gildas 7 سال پیش
والد
کامیت
327b021a3c
1فایلهای تغییر یافته به همراه30 افزوده شده و 30 حذف شده
  1. 30 30
      lib/single-file/frame-tree.js

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

@@ -29,6 +29,7 @@ this.frameTree = this.frameTree || (() => {
 	const TARGET_ORIGIN = "*";
 	const TIMEOUT_INIT_REQUEST_MESSAGE = 500;
 	const TOP_WINDOW_ID = "0";
+	const WINDOW_ID_SEPARATOR = ".";
 	const TOP_WINDOW = window == top;
 
 	let sessions = new Map(), windowId;
@@ -71,19 +72,7 @@ this.frameTree = this.frameTree || (() => {
 		const frameElements = document.querySelectorAll(FRAMES_CSS_SELECTOR);
 		if (!TOP_WINDOW) {
 			windowId = message.windowId;
-			const docData = docHelper.preProcessDoc(document, window, message.options);
-			const content = docHelper.serialize(document);
-			docHelper.postProcessDoc(document, window, message.options);
-			const framesData = [{
-				windowId,
-				content,
-				baseURI: document.baseURI.split("#")[0],
-				title: document.title,
-				stylesheetContents: docData.stylesheetContents,
-				canvasData: docData.canvasData,
-				processed: true,
-			}];
-			sendInitResponse({ framesData, sessionId });
+			sendInitResponse({ framesData: [getFrameData(document, window, windowId, message.options)], sessionId });
 		}
 		processFrames(frameElements, message.options, windowId, sessionId);
 	}
@@ -108,7 +97,7 @@ this.frameTree = this.frameTree || (() => {
 			const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
 			if (!remainingFrames) {
 				sessions.delete(message.sessionId);
-				windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length);
+				windowData.frames = windowData.frames.sort((frame1, frame2) => frame2.windowId.split(WINDOW_ID_SEPARATOR).length - frame1.windowId.split(WINDOW_ID_SEPARATOR).length);
 				if (windowData.resolve) {
 					windowData.resolve(windowData.frames);
 				}
@@ -124,43 +113,39 @@ this.frameTree = this.frameTree || (() => {
 	}
 
 	function processFramesAsync(frameElements, options, parentWindowId, sessionId) {
-		let framesData = [];
+		const framesData = [];
 		frameElements.forEach((frameElement, frameIndex) => {
-			const windowId = parentWindowId + "." + frameIndex;
+			const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
 			frameElement.setAttribute(docHelper.windowIdAttributeName(options.sessionId), windowId);
 			framesData.push({ windowId });
 			if (!frameElement.contentDocument) {
 				try {
-					frameElement.contentWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: INIT_REQUEST_MESSAGE, windowId, sessionId, options }), TARGET_ORIGIN);
+					sendMessage(frameElement.contentWindow, { method: INIT_REQUEST_MESSAGE, windowId, sessionId, options });
 				} catch (error) {
 					/* ignored */
 				}
 			}
-			timeout.set(() => sendInitResponse({ framesData: [{ windowId, processed: true, timeout: true }], windowId, sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
+			timeout.set(() => sendInitResponse({ framesData: [{ windowId, processed: true, timeout: true }], windowId: parentWindowId, sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
 		});
-		sendInitResponse({ framesData, parentWindowId, sessionId });
+		sendInitResponse({ framesData, windowId: parentWindowId, sessionId });
 	}
 
 	function processFramesSync(frameElements, options, parentWindowId, sessionId) {
-		let framesData = [];
+		const framesData = [];
 		frameElements.forEach((frameElement, frameIndex) => {
-			const windowId = parentWindowId + "." + frameIndex;
+			const windowId = parentWindowId + WINDOW_ID_SEPARATOR + 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);
+					framesData.push(getFrameData(frameDoc, frameWindow, windowId, options));
 				} catch (error) {
 					framesData.push({ windowId, processed: true });
 				}
 			}
 		});
-		sendInitResponse({ framesData, parentWindowId, sessionId });
+		sendInitResponse({ framesData, windowId: parentWindowId, sessionId });
 	}
 
 	function sendInitResponse(message) {
@@ -168,11 +153,26 @@ this.frameTree = this.frameTree || (() => {
 		try {
 			top.frameTree.initResponse(message);
 		} catch (error) {
+			sendMessage(top, message, true);
+		}
+	}
+
+	function sendMessage(targetWindow, message, useChannel) {
+		if (useChannel) {
 			const channel = new MessageChannel();
-			const port = channel.port1;
-			top.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: INIT_RESPONSE_MESSAGE }), TARGET_ORIGIN, [channel.port2]);
-			port.postMessage(message);
+			targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method }), TARGET_ORIGIN, [channel.port2]);
+			channel.port1.postMessage(message);
+		} else {
+			targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
 		}
 	}
 
+	function getFrameData(document, window, windowId, options) {
+		const docData = docHelper.preProcessDoc(document, window, options);
+		const content = docHelper.serialize(document);
+		docHelper.postProcessDoc(document, window, options);
+		const baseURI = document.baseURI.split("#")[0];
+		return { windowId, content, baseURI, title: document.title, stylesheetContents: docData.stylesheetContents, canvasData: docData.canvasData, processed: true };
+	}
+
 })();