-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters