From 70c7d84bdb2a26eb1f990fdad746568f54c7ccf9 Mon Sep 17 00:00:00 2001 From: Tanuj Mittal Date: Fri, 25 Sep 2020 14:49:11 -0700 Subject: [PATCH] Do not fail when trying to find DE window with cross origin (#231) * Do not fail when trying to find DE window with cross origin * Fix lint errors --- src/Common/MessageHandler.ts | 19 +++++++++---------- src/Utils/WindowUtils.ts | 22 ++++++++++++++-------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Common/MessageHandler.ts b/src/Common/MessageHandler.ts index 31317800a..1932739e9 100644 --- a/src/Common/MessageHandler.ts +++ b/src/Common/MessageHandler.ts @@ -49,16 +49,15 @@ export function sendCachedDataMessage( export function sendMessage(data: any): void { if (canSendMessage()) { - const dataExplorerWindow = getDataExplorerWindow(window); - if (dataExplorerWindow) { - dataExplorerWindow.parent.postMessage( - { - signature: "pcIframe", - data: data - }, - dataExplorerWindow.document.referrer - ); - } + // We try to find data explorer window first, then fallback to current window + const portalChildWindow = getDataExplorerWindow(window) || window; + portalChildWindow.parent.postMessage( + { + signature: "pcIframe", + data: data + }, + portalChildWindow.document.referrer + ); } } diff --git a/src/Utils/WindowUtils.ts b/src/Utils/WindowUtils.ts index e672f288c..909cc9a3f 100644 --- a/src/Utils/WindowUtils.ts +++ b/src/Utils/WindowUtils.ts @@ -2,15 +2,21 @@ export const getDataExplorerWindow = (currentWindow: Window): Window | undefined // Start with the current window and traverse up the parent hierarchy to find a window // with `dataExplorerPlatform` property let dataExplorerWindow: Window | undefined = currentWindow; - // 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 - while (dataExplorerWindow && (dataExplorerWindow as any).dataExplorerPlatform === undefined) { - // If a window does not have a parent, its parent property is a reference to itself. - if (dataExplorerWindow.parent === dataExplorerWindow) { - dataExplorerWindow = undefined; - } else { - dataExplorerWindow = dataExplorerWindow.parent; + + try { + // 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 + while (dataExplorerWindow && (dataExplorerWindow as any).dataExplorerPlatform === undefined) { + // If a window does not have a parent, its parent property is a reference to itself. + if (dataExplorerWindow.parent === dataExplorerWindow) { + dataExplorerWindow = undefined; + } else { + dataExplorerWindow = dataExplorerWindow.parent; + } } + } catch (error) { + // This can happen if we come across parent from a different origin + dataExplorerWindow = undefined; } return dataExplorerWindow;