diff --git a/src/app.js b/src/app.js index d1c2c57b..c31f22ee 100644 --- a/src/app.js +++ b/src/app.js @@ -1,6 +1,7 @@ import 'setimmediate'; import './lib/TabFreezePrevention'; import './lib/patchGetUserMedia'; +import './lib/patchOpenWindow'; import React from 'react'; import ReactDOM from 'react-dom'; @@ -21,7 +22,7 @@ const clientIdFromParams = pathParams.clientId || pathParams.appKey; const clientSecretFromParams = pathParams.clientSecret || pathParams.appSecret; const authProxy = pathParams.authProxy; const enableDiscovery = !!pathParams.discovery; -const discoverAppServer = pathParams.discoverAppServer; + function getAppServer() { if ( pathParams.appServer && @@ -38,9 +39,23 @@ const apiConfig = { clientSecret: (clientIdFromParams ? clientSecretFromParams : defaultApiConfig.appSecret), server: getAppServer(), }; + +function getDiscoveryServer() { + const discoveryServer = pathParams.discoverAppServer || apiConfig.server; + if ([ + 'https://platform.ringcentral.com', + 'https://platform.devtest.ringcentral.com', + 'https://discovery.ringcentral.biz', + 'https://discovery.ringcentral.com', + 'https://platform.ringcentral.biz', + ].indexOf(discoveryServer) > -1) { + return discoveryServer; + } + return defaultApiConfig.server; +} if (enableDiscovery) { apiConfig.enableDiscovery = enableDiscovery; - apiConfig.discoveryServer = discoverAppServer || apiConfig.server; + apiConfig.discoveryServer = getDiscoveryServer(); } if (!authProxy && pathParams.appKey) { console.warn('appKey is deprecated, please change to clientId. https://ringcentral.github.io/ringcentral-embeddable/docs/config/client-id/'); diff --git a/src/lib/Adapter/index.js b/src/lib/Adapter/index.js index cbeac550..0c297d40 100644 --- a/src/lib/Adapter/index.js +++ b/src/lib/Adapter/index.js @@ -1,9 +1,9 @@ import classnames from 'classnames'; import url from 'url'; -import popWindow from '@ringcentral-integration/widgets/lib/popWindow'; import AdapterCore from '@ringcentral-integration/widgets/lib/AdapterCore'; import { isSafari } from '@ringcentral-integration/utils'; +import popWindow from '../popWindow'; import parseUri from '../parseUri'; import messageTypes from './messageTypes'; import requestWithPostMessage from '../requestWithPostMessage'; diff --git a/src/lib/patchOpenWindow.js b/src/lib/patchOpenWindow.js new file mode 100644 index 00000000..1e6eef81 --- /dev/null +++ b/src/lib/patchOpenWindow.js @@ -0,0 +1,15 @@ +try { + if (!window.__originalOpen) { + window.__originalOpen = window.open; + window.open = function(url, id, options) { + if (typeof url === 'string' && url.indexOf('javascript') > -1) { + throw new Error('Invalid open window uri'); + } + return window.__originalOpen(url, id, options); + } + } +} catch (e) { + console.error(e); +} + + diff --git a/src/lib/popWindow.ts b/src/lib/popWindow.ts new file mode 100644 index 00000000..f460d59c --- /dev/null +++ b/src/lib/popWindow.ts @@ -0,0 +1,35 @@ +export function popWindow(url: string, id: string, w: number, h: number) { + if (url.indexOf('javascript') > 0) { + throw new Error('Invalid window open url'); + } + // Fixes dual-screen position Most browsers Firefox + const dualScreenLeft = + window.screenLeft !== undefined + ? window.screenLeft + : (window.screen as any).left; + const dualScreenTop = + window.screenTop !== undefined + ? window.screenTop + : (window.screen as any).top; + + const width = window.screen.width || window.outerWidth; + const height = window.screen.height || window.innerHeight; + const left = width / 2 - w / 2 + dualScreenLeft; + const top = height / 2 - h / 2 + dualScreenTop; + + const newWindow = window.open( + url, + id, + `scrollbars=yes, width=${w}, height=${h}, top=${top}, left=${left}`, + ); + + // Puts focus on the newWindow + try { + newWindow?.focus(); + } catch (error) { + /* ignore error */ + } + return newWindow; +} + +export default popWindow;