|
|
@@ -33,6 +33,7 @@ this.frameTree = this.frameTree || (() => {
|
|
|
|
|
|
if (TOP_WINDOW) {
|
|
|
sessions = new Map();
|
|
|
+ windowId = "0";
|
|
|
}
|
|
|
addEventListener("message", event => {
|
|
|
if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX + "::")) {
|
|
|
@@ -50,20 +51,21 @@ this.frameTree = this.frameTree || (() => {
|
|
|
options = JSON.parse(JSON.stringify(options));
|
|
|
return new Promise(resolve => {
|
|
|
sessions.set(sessionId, { frames: [], resolve });
|
|
|
- initRequest({ windowId: "0", sessionId, options });
|
|
|
+ initRequest({ windowId, sessionId, options });
|
|
|
});
|
|
|
- }
|
|
|
+ },
|
|
|
+ initResponse
|
|
|
};
|
|
|
|
|
|
function initRequest(message) {
|
|
|
const sessionId = message.sessionId;
|
|
|
- windowId = message.windowId;
|
|
|
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);
|
|
|
- top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({
|
|
|
+ callTopInitResponse({
|
|
|
method: INIT_RESPONSE_MESSAGE, framesData: [{
|
|
|
windowId,
|
|
|
content,
|
|
|
@@ -71,45 +73,38 @@ this.frameTree = this.frameTree || (() => {
|
|
|
title: document.title,
|
|
|
emptyStyleRulesText: docData.emptyStyleRulesText,
|
|
|
canvasData: docData.canvasData,
|
|
|
- processed: true
|
|
|
+ processed: true,
|
|
|
}], sessionId
|
|
|
- }), "*");
|
|
|
+ });
|
|
|
}
|
|
|
- processFrames(frameElements, message.options, windowId, sessionId, window);
|
|
|
+ processFrames(frameElements, message.options, windowId, sessionId);
|
|
|
}
|
|
|
|
|
|
function initResponse(message) {
|
|
|
- if (TOP_WINDOW) {
|
|
|
- const windowData = sessions.get(message.sessionId);
|
|
|
- if (windowData) {
|
|
|
- message.framesData.forEach(messageFrameData => {
|
|
|
- let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
|
|
|
- if (!frameData) {
|
|
|
- frameData = { windowId: messageFrameData.windowId };
|
|
|
- windowData.frames.push(frameData);
|
|
|
- }
|
|
|
- frameData.content = messageFrameData.content;
|
|
|
- frameData.baseURI = messageFrameData.baseURI;
|
|
|
- frameData.title = messageFrameData.title;
|
|
|
- frameData.emptyStyleRulesText = messageFrameData.emptyStyleRulesText;
|
|
|
- frameData.canvasData = messageFrameData.canvasData;
|
|
|
- frameData.processed = messageFrameData.processed;
|
|
|
- });
|
|
|
- const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
|
|
|
- if (!remainingFrames) {
|
|
|
- sessions.delete(message.sessionId);
|
|
|
- windowData.resolve(windowData.frames.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length));
|
|
|
+ const windowData = sessions.get(message.sessionId);
|
|
|
+ if (windowData) {
|
|
|
+ message.framesData.forEach(messageFrameData => {
|
|
|
+ let frameData = windowData.frames.find(frameData => messageFrameData.windowId == frameData.windowId);
|
|
|
+ if (!frameData) {
|
|
|
+ frameData = { windowId: messageFrameData.windowId };
|
|
|
+ windowData.frames.push(frameData);
|
|
|
}
|
|
|
+ frameData.content = messageFrameData.content;
|
|
|
+ frameData.baseURI = messageFrameData.baseURI;
|
|
|
+ frameData.title = messageFrameData.title;
|
|
|
+ frameData.emptyStyleRulesText = messageFrameData.emptyStyleRulesText;
|
|
|
+ frameData.canvasData = messageFrameData.canvasData;
|
|
|
+ frameData.processed = messageFrameData.processed;
|
|
|
+ });
|
|
|
+ const remainingFrames = windowData.frames.filter(frameData => !frameData.processed).length;
|
|
|
+ if (!remainingFrames) {
|
|
|
+ sessions.delete(message.sessionId);
|
|
|
+ windowData.resolve(windowData.frames.sort((frame1, frame2) => frame2.windowId.split(".").length - frame1.windowId.split(".").length));
|
|
|
}
|
|
|
- } else {
|
|
|
- windowId = message.windowId;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- function processFrames(frameElements, options, windowId, sessionId, window) {
|
|
|
- if (!isTopWindow(window)) {
|
|
|
- window.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE, windowId, sessionId }), "*");
|
|
|
- }
|
|
|
+ function processFrames(frameElements, options, windowId, sessionId) {
|
|
|
let framesData = [];
|
|
|
frameElements.forEach((frameElement, frameIndex) => {
|
|
|
const frameWindowId = windowId + "." + frameIndex;
|
|
|
@@ -128,7 +123,7 @@ this.frameTree = this.frameTree || (() => {
|
|
|
windowId: frameWindowId, sessionId
|
|
|
}), "*"), TIMEOUT_INIT_REQUEST_MESSAGE);
|
|
|
});
|
|
|
- top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId }), "*");
|
|
|
+ callTopInitResponse({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId });
|
|
|
if (frameElements.length) {
|
|
|
framesData = [];
|
|
|
frameElements.forEach((frameElement, frameIndex) => {
|
|
|
@@ -137,7 +132,7 @@ this.frameTree = this.frameTree || (() => {
|
|
|
const frameDoc = frameElement.contentDocument;
|
|
|
if (frameDoc) {
|
|
|
try {
|
|
|
- processFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, frameWindowId, sessionId, frameWindow);
|
|
|
+ processFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, frameWindowId, sessionId);
|
|
|
const docData = docHelper.preProcessDoc(frameDoc, frameWindow, options);
|
|
|
framesData.push({
|
|
|
windowId: frameWindowId,
|
|
|
@@ -156,9 +151,16 @@ this.frameTree = this.frameTree || (() => {
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
});
|
|
|
- top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId }), "*");
|
|
|
+ callTopInitResponse({ method: INIT_RESPONSE_MESSAGE, framesData, windowId, sessionId });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function callTopInitResponse(message) {
|
|
|
+ try {
|
|
|
+ top.frameTree.initResponse(message);
|
|
|
+ } catch (error) {
|
|
|
+ top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify(message), "*");
|
|
|
}
|
|
|
}
|
|
|
|