CHANGE postmessage frame lookup

This commit is contained in:
Norbert de Langen 2020-04-10 16:08:56 +02:00
parent 19c2420db8
commit 2752631f22
No known key found for this signature in database
GPG Key ID: 976651DA156C2825

View File

@ -170,7 +170,7 @@ export class PostmsgTransport {
? `<span style="color: #FF4785">${event.type}</span>`
: `<span style="color: #FFAE00">${event.type}</span>`;
event.source = source || getEventSourceUrl(rawEvent);
event.source = source || getEventSourceUrl(rawEvent, this.getFrames());
if (!event.source) {
logger.error(
@ -194,9 +194,7 @@ export class PostmsgTransport {
}
}
const getEventSourceUrl = (event: MessageEvent) => {
const frames: HTMLIFrameElement[] = [...document.getElementsByTagName('iframe')];
const getEventSourceUrl = (event: MessageEvent, frames: HTMLIFrameElement[]) => {
// try to find the originating iframe by matching it's contentWindow
// This might not be cross-origin safe
const [frame, ...remainder] = frames.filter((element) => {
@ -223,9 +221,13 @@ const getEventSourceUrl = (event: MessageEvent) => {
return null;
}
const src = frame.getAttribute('src');
const { origin, pathname } = new URL(src, document.location);
return origin + pathname;
if (frame) {
const src = frame.getAttribute('src');
const { origin, pathname } = new URL(src, document.location);
return origin + pathname;
}
return event.origin;
};
/**