Sfoglia il codice sorgente

use MessageChannels to retrieve frame contents

Gildas 7 anni fa
parent
commit
e46240c3af
1 ha cambiato i file con 13 aggiunte e 9 eliminazioni
  1. 13 9
      lib/single-file/frame-tree.js

+ 13 - 9
lib/single-file/frame-tree.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
  */
 
 
-/* global window, top, document, addEventListener, docHelper, timeout */
+/* global window, top, document, addEventListener, docHelper, timeout, MessageChannel */
 
 
 this.frameTree = this.frameTree || (() => {
 this.frameTree = this.frameTree || (() => {
 
 
@@ -40,7 +40,8 @@ this.frameTree = this.frameTree || (() => {
 			if (message.method == INIT_REQUEST_MESSAGE) {
 			if (message.method == INIT_REQUEST_MESSAGE) {
 				initRequest(message);
 				initRequest(message);
 			} else if (message.method == INIT_RESPONSE_MESSAGE) {
 			} else if (message.method == INIT_RESPONSE_MESSAGE) {
-				initResponse(message);
+				const port = event.ports[0];
+				port.onmessage = event => initResponse(event.data);
 			}
 			}
 		}
 		}
 	}, false);
 	}, false);
@@ -72,7 +73,7 @@ this.frameTree = this.frameTree || (() => {
 			const content = docHelper.serialize(document);
 			const content = docHelper.serialize(document);
 			docHelper.postProcessDoc(document, window, message.options);
 			docHelper.postProcessDoc(document, window, message.options);
 			callTopInitResponse({
 			callTopInitResponse({
-				method: INIT_RESPONSE_MESSAGE, framesData: [{
+				framesData: [{
 					windowId,
 					windowId,
 					content,
 					content,
 					baseURI: document.baseURI.split("#")[0],
 					baseURI: document.baseURI.split("#")[0],
@@ -127,13 +128,12 @@ this.frameTree = this.frameTree || (() => {
 					/* ignored */
 					/* ignored */
 				}
 				}
 			}
 			}
-			timeout.set(() => top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({
-				method: INIT_RESPONSE_MESSAGE,
+			timeout.set(() => callTopInitResponse({
 				framesData: [{ windowId: frameWindowId, processed: true, timeout: true }],
 				framesData: [{ windowId: frameWindowId, processed: true, timeout: true }],
 				windowId: frameWindowId, sessionId
 				windowId: frameWindowId, sessionId
-			}), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
+			}, "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
 		});
 		});
-		callTopInitResponse({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId });
+		callTopInitResponse({ framesData, windowId, sessionId });
 		if (frameElements.length) {
 		if (frameElements.length) {
 			framesData = [];
 			framesData = [];
 			frameElements.forEach((frameElement, frameIndex) => {
 			frameElements.forEach((frameElement, frameIndex) => {
@@ -162,15 +162,19 @@ this.frameTree = this.frameTree || (() => {
 					}
 					}
 				}
 				}
 			});
 			});
-			callTopInitResponse({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId });
+			callTopInitResponse({ framesData, windowId, sessionId });
 		}
 		}
 	}
 	}
 
 
 	function callTopInitResponse(message) {
 	function callTopInitResponse(message) {
+		message.method = INIT_RESPONSE_MESSAGE;
 		try {
 		try {
 			top.frameTree.initResponse(message);
 			top.frameTree.initResponse(message);
 		} catch (error) {
 		} catch (error) {
-			top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify(message), "*");
+			const channel = new MessageChannel();
+			const port = channel.port1;
+			top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE }), "*", [channel.port2]);
+			port.postMessage(message);
 		}
 		}
 	}
 	}