From c23e20028998cb99e3a4801b9205bc0ecacc75e1 Mon Sep 17 00:00:00 2001 From: Alexander Salmin Date: Thu, 22 Jul 2021 16:55:10 +0400 Subject: [PATCH] =?UTF-8?q?chore:=20iframe=20=D0=BE=D1=82=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/iframe.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 68 +-------------------------------------------------- 2 files changed, 68 insertions(+), 67 deletions(-) create mode 100644 src/iframe.ts diff --git a/src/iframe.ts b/src/iframe.ts new file mode 100644 index 0000000..a96a1ee --- /dev/null +++ b/src/iframe.ts @@ -0,0 +1,67 @@ +import { AssistantPostMessage } from './typings'; + +const inIframe = () => { + try { + return window.self !== window.top; + } catch (e) { + return true; + } +}; + +if (typeof window !== 'undefined' && inIframe()) { + const postMessage = (action: AssistantPostMessage) => { + window.top.postMessage(JSON.stringify(action), '*'); + }; + + window.AssistantHost = { + sendDataContainer(json: string) { + postMessage({ type: 'sendDataContainer', payload: json }); + }, + close() { + postMessage({ type: 'close' }); + }, + sendData(json: string) { + postMessage({ type: 'sendData', payload: json }); + }, + setSuggest(suggests: string) { + postMessage({ type: 'setSuggest', payload: suggests }); + }, + ready() { + postMessage({ type: 'ready' }); + }, + }; + + window.addEventListener('message', (e) => { + try { + if (typeof e.data === 'string') { + const data = JSON.parse(e.data); + + switch (data.type) { + case 'onData': + window.AssistantClient?.onData?.(data.payload); + break; + case 'onRequestState': { + const state = window.AssistantClient?.onRequestState?.(); + postMessage({ type: 'state', payload: state, requestId: data.requestId }); + break; + } + case 'onRequestRecoveryState': { + const recoverystate = window.AssistantClient?.onRequestRecoveryState?.(); + postMessage({ type: 'recoveryState', payload: recoverystate }); + break; + } + case 'onStart': + window.AssistantClient?.onStart?.(); + break; + default: + // eslint-disable-next-line no-console + console.error(e, 'Unknown parsed message'); + break; + } + } + } catch (err) { + // eslint-disable-next-line no-console + console.error(err, 'Unknown message'); + } + }); +} diff --git a/src/index.ts b/src/index.ts index 441d51e..e3fa2ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,11 +11,11 @@ import { AssistantClientCommand, AssistantSmartAppError, AssistantSmartAppCommand, - AssistantPostMessage, } from './typings'; import { createNanoEvents } from './nanoevents'; import { initializeAssistantSDK, InitializeAssistantSDKParams } from './dev'; import { createNanoObservable, ObserverFunc } from './nanoobservable'; +import './iframe'; export interface AssistantEvents { start: () => void; @@ -34,72 +34,6 @@ export interface SendDataParams { requestId?: string; } -function inIframe() { - try { - return window.self !== window.top; - } catch (e) { - return true; - } -} - -if (typeof window !== 'undefined' && inIframe()) { - const postMessage = (action: AssistantPostMessage) => { - window.top.postMessage(JSON.stringify(action), '*'); - }; - - window.AssistantHost = { - sendDataContainer(json: string) { - postMessage({ type: 'sendDataContainer', payload: json }); - }, - close() { - postMessage({ type: 'close' }); - }, - sendData(json: string) { - postMessage({ type: 'sendData', payload: json }); - }, - setSuggest(suggests: string) { - postMessage({ type: 'setSuggest', payload: suggests }); - }, - ready() { - postMessage({ type: 'ready' }); - }, - }; - - window.addEventListener('message', (e) => { - try { - if (typeof e.data === 'string') { - const data = JSON.parse(e.data); - - switch (data.type) { - case 'onData': - window.AssistantClient?.onData?.(data.payload); - break; - case 'onRequestState': { - const state = window.AssistantClient?.onRequestState?.(); - postMessage({ type: 'state', payload: state, requestId: data.requestId }); - break; - } - case 'onRequestRecoveryState': { - const recoverystate = window.AssistantClient?.onRequestRecoveryState?.(); - postMessage({ type: 'recoveryState', payload: recoverystate }); - break; - } - case 'onStart': - window.AssistantClient?.onStart?.(); - break; - default: - // eslint-disable-next-line no-console - console.error(e, 'Unknown parsed message'); - break; - } - } - } catch (err) { - // eslint-disable-next-line no-console - console.error(err, 'Unknown message'); - } - }); -} - export const createAssistant = ({ getState, getRecoveryState,