|
|
@@ -86,7 +86,7 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
|
|
|
createFrameResponseTimeout(message.sessionId, message.windowId);
|
|
|
} else if (message.method == CLEANUP_REQUEST_MESSAGE) {
|
|
|
cleanupRequest(message);
|
|
|
- } else if ((!browser || !browser.runtime) && message.method == INIT_RESPONSE_MESSAGE) {
|
|
|
+ } else if (message.method == INIT_RESPONSE_MESSAGE && sessions.get(message.sessionId)) {
|
|
|
const port = event.ports[0];
|
|
|
port.onmessage = event => initResponse(event.data);
|
|
|
}
|
|
|
@@ -94,28 +94,46 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
|
|
|
}, true);
|
|
|
return {
|
|
|
getAsync: options => {
|
|
|
- const sessionId = options.sessionId || 0;
|
|
|
+ const sessionId = getNewSessionId();
|
|
|
options = JSON.parse(JSON.stringify(options));
|
|
|
return new Promise(resolve => {
|
|
|
- sessions.set(sessionId, { frames: [], requestTimeouts: {}, responseTimeouts: {}, resolve });
|
|
|
+ sessions.set(sessionId, {
|
|
|
+ frames: [],
|
|
|
+ requestTimeouts: {},
|
|
|
+ responseTimeouts: {},
|
|
|
+ resolve: frames => {
|
|
|
+ frames.sessionId = sessionId;
|
|
|
+ resolve(frames);
|
|
|
+ }
|
|
|
+ });
|
|
|
initRequestAsync({ windowId, sessionId, options });
|
|
|
});
|
|
|
},
|
|
|
getSync: options => {
|
|
|
- const sessionId = options.sessionId || 0;
|
|
|
+ const sessionId = getNewSessionId();
|
|
|
options = JSON.parse(JSON.stringify(options));
|
|
|
- sessions.set(sessionId, { frames: [], requestTimeouts: {}, responseTimeouts: {} });
|
|
|
+ sessions.set(sessionId, {
|
|
|
+ frames: [],
|
|
|
+ requestTimeouts: {},
|
|
|
+ responseTimeouts: {}
|
|
|
+ });
|
|
|
initRequestSync({ windowId, sessionId, options });
|
|
|
- return sessions.get(sessionId).frames;
|
|
|
+ const frames = sessions.get(sessionId).frames;
|
|
|
+ frames.sessionId = sessionId;
|
|
|
+ return frames;
|
|
|
},
|
|
|
- cleanup: options => {
|
|
|
- const sessionId = options.sessionId || 0;
|
|
|
+ cleanup: sessionId => {
|
|
|
+ sessions.delete(sessionId);
|
|
|
cleanupRequest({ windowId, sessionId, options: { sessionId } });
|
|
|
},
|
|
|
initResponse,
|
|
|
TIMEOUT_INIT_REQUEST_MESSAGE
|
|
|
};
|
|
|
|
|
|
+ function getNewSessionId() {
|
|
|
+ return window.crypto.getRandomValues(new Uint32Array(32)).join("");
|
|
|
+ }
|
|
|
+
|
|
|
function initRequestSync(message) {
|
|
|
const waitForUserScript = singlefile.lib.helper.waitForUserScript;
|
|
|
const sessionId = message.sessionId;
|
|
|
@@ -325,7 +343,7 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
|
|
|
} else {
|
|
|
if (useChannel) {
|
|
|
const channel = new MessageChannel();
|
|
|
- targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method }), TARGET_ORIGIN, [channel.port2]);
|
|
|
+ targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify({ method: message.method, sessionId: message.sessionId }), TARGET_ORIGIN, [channel.port2]);
|
|
|
channel.port1.postMessage(message);
|
|
|
} else {
|
|
|
targetWindow.postMessage(MESSAGE_PREFIX + JSON.stringify(message), TARGET_ORIGIN);
|