Do not fail when trying to find DE window with cross origin

This commit is contained in:
Tanuj Mittal
2020-09-25 13:49:46 -07:00
parent a04afc48e3
commit 823cbc4229
+10 -5
View File
@@ -48,24 +48,25 @@ export function sendCachedDataMessage<TResponseDataModel>(
export function sendMessage(data: any): void { export function sendMessage(data: any): void {
if (canSendMessage()) { if (canSendMessage()) {
const dataExplorerWindow = getDataExplorerWindow(window); // We try to find data explorer window first, then fallback to current window
if (dataExplorerWindow) { const portalChildWindow = getDataExplorerWindow(window) || window;
dataExplorerWindow.parent.postMessage( portalChildWindow.parent.postMessage(
{ {
signature: "pcIframe", signature: "pcIframe",
data: data data: data
}, },
dataExplorerWindow.document.referrer portalChildWindow.document.referrer
); );
} }
} }
}
// Only exported for unit tests // Only exported for unit tests
export const getDataExplorerWindow = (currentWindow: Window): Window | undefined => { export const getDataExplorerWindow = (currentWindow: Window): Window | undefined => {
// Start with the current window and traverse up the parent hierarchy to find a window // Start with the current window and traverse up the parent hierarchy to find a window
// with `dataExplorerPlatform` property // with `dataExplorerPlatform` property
let dataExplorerWindow: Window | undefined = currentWindow; let dataExplorerWindow: Window | undefined = currentWindow;
try {
// TODO: Need to `any` here since the window imports Explorer which can't be in strict mode yet // TODO: Need to `any` here since the window imports Explorer which can't be in strict mode yet
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
while (dataExplorerWindow && (dataExplorerWindow as any).dataExplorerPlatform == undefined) { while (dataExplorerWindow && (dataExplorerWindow as any).dataExplorerPlatform == undefined) {
@@ -76,6 +77,10 @@ export const getDataExplorerWindow = (currentWindow: Window): Window | undefined
dataExplorerWindow = dataExplorerWindow.parent; dataExplorerWindow = dataExplorerWindow.parent;
} }
} }
} catch (error) {
// This can happen if we come across parent from a different origin
dataExplorerWindow = undefined;
}
return dataExplorerWindow; return dataExplorerWindow;
}; };