Просмотр исходного кода

unified bg and window messages format

Gildas 7 лет назад
Родитель
Сommit
9e53ce5679
1 измененных файлов с 13 добавлено и 25 удалено
  1. 13 25
      lib/frame-tree/content/frame-tree.js

+ 13 - 25
lib/frame-tree/content/frame-tree.js

@@ -62,11 +62,11 @@ this.FrameTree = (() => {
 	addEventListener("message", event => {
 		if (typeof event.data === "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
 			const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
-			if (message.initRequest) {
+			if (message.method == "initRequest") {
 				initRequest(message);
-			} else if (message.initResponse) {
+			} else if (message.method == "initResponse") {
 				initResponse(message);
-			} else if (message.getDataResponse) {
+			} else if (message.method == "getDataResponse") {
 				getDataResponse(message);
 			}
 		}
@@ -78,19 +78,14 @@ this.FrameTree = (() => {
 			return new Promise(resolve => {
 				dataRequestCallbacks.set(frameData.windowId, resolve);
 				if (frameData.sameDomain) {
-					top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({
-						getDataRequest: true,
-						windowId: frameData.windowId
-					}), "*");
+					top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataRequest", windowId: frameData.windowId }), "*");
 				} else {
 					browser.runtime.sendMessage({
 						method: "FrameTree.getDataRequest",
 						windowId: frameData.windowId
 					});
 				}
-				frameData.getDataResponseTimeout = setTimeout(() => {
-					top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ getDataResponse: true, windowId: frameData.windowId }), "*");
-				}, TIMEOUT_DATA_RESPONSE_MESSAGE);
+				frameData.getDataResponseTimeout = setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: frameData.windowId }), "*"), TIMEOUT_DATA_RESPONSE_MESSAGE);
 			});
 		}));
 		return framesData.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length);
@@ -103,7 +98,7 @@ this.FrameTree = (() => {
 		if (frameElements.length) {
 			setFramesWinId(MESSAGE_PREFIX, frameElements, FrameTree.index, FrameTree.windowId, window);
 		} else {
-			top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ initResponse: true, framesData: [], windowId: FrameTree.windowId, index: FrameTree.index }), "*");
+			top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: FrameTree.windowId, index: FrameTree.index }), "*");
 		}
 	}
 
@@ -130,7 +125,7 @@ this.FrameTree = (() => {
 	function setFramesWinId(MESSAGE_PREFIX, frameElements, index, windowId, win) {
 		const framesData = [];
 		if (win != top) {
-			win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ initResponse: true, windowId, index }), "*");
+			win.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", windowId, index }), "*");
 		}
 		frameElements.forEach((frameElement, index) => {
 			let src, sameDomain;
@@ -142,7 +137,7 @@ this.FrameTree = (() => {
 			}
 			framesData.push({ sameDomain, src, index, windowId: windowId + "." + index });
 		});
-		top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ initResponse: true, framesData, windowId, index }), "*");
+		top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, index }), "*");
 		frameElements.forEach((frameElement, index) => {
 			const frameWinId = windowId + "." + index;
 			let frameDoc, frameWindow, topWindow;
@@ -157,24 +152,17 @@ this.FrameTree = (() => {
 				setFramesWinId(MESSAGE_PREFIX, frameDoc.querySelectorAll("iframe, frame"), index, frameWinId, frameWindow);
 				topWindow.addEventListener("message", onMessage, false);
 			} else if (frameWindow) {
-				frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ initRequest: true, windowId: frameWinId, index }), "*");
-				setTimeout(() => {
-					top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ initResponse: true, framesData: [], windowId: frameWinId, index }), "*");
-				}, TIMEOUT_POST_MESSAGE);
+				frameWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initRequest", windowId: frameWinId, index }), "*");
+				setTimeout(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData: [], windowId: frameWinId, index }), "*"), TIMEOUT_POST_MESSAGE);
 			}
 
 			function onMessage(event) {
 				if (typeof event.data === "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
 					const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length + 2));
-					if (message.getDataRequest && message.windowId == frameWinId) {
+					if (message.method == "getDataRequest" && message.windowId == frameWinId) {
 						topWindow.removeEventListener("message", onMessage, false);
-						topWindow.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({
-							getDataResponse: true,
-							windowId: message.windowId,
-							content: getDoctype(frameDoc) + frameDoc.documentElement.outerHTML,
-							baseURI: frameDoc.baseURI,
-							title: document.title
-						}), "*");
+						const content = getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
+						top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title }), "*");
 					}
 				}
 			}