✨ (logic) Add execute in parent window context for code block
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "typebot-js",
|
||||
"version": "2.2.9",
|
||||
"version": "2.2.10",
|
||||
"main": "dist/index.js",
|
||||
"unpkg": "dist/index.umd.min.js",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { DataFromTypebot, IframeCallbacks, IframeParams } from '../types'
|
||||
import { TypebotPostMessageData, IframeCallbacks, IframeParams } from '../types'
|
||||
import './style.css'
|
||||
|
||||
export const createIframe = ({
|
||||
@ -24,8 +24,8 @@ export const createIframe = ({
|
||||
iframe.setAttribute('id', uniqueId)
|
||||
if (backgroundColor) iframe.style.backgroundColor = backgroundColor
|
||||
iframe.classList.add('typebot-iframe')
|
||||
const { onNewVariableValue, onVideoPlayed } = iframeParams
|
||||
listenForTypebotMessages({ onNewVariableValue, onVideoPlayed })
|
||||
const { onNewVariableValue } = iframeParams
|
||||
listenForTypebotMessages({ onNewVariableValue })
|
||||
return iframe
|
||||
}
|
||||
|
||||
@ -50,14 +50,17 @@ const parseStarterVariables = (starterVariables?: {
|
||||
|
||||
export const listenForTypebotMessages = (callbacks: IframeCallbacks) => {
|
||||
window.addEventListener('message', (event) => {
|
||||
const data = event.data as { from?: 'typebot' } & DataFromTypebot
|
||||
const data = event.data as { from?: 'typebot' } & TypebotPostMessageData
|
||||
if (data.from === 'typebot') processMessage(event.data, callbacks)
|
||||
})
|
||||
}
|
||||
|
||||
const processMessage = (data: DataFromTypebot, callbacks: IframeCallbacks) => {
|
||||
const processMessage = (
|
||||
data: TypebotPostMessageData,
|
||||
callbacks: IframeCallbacks
|
||||
) => {
|
||||
if (data.redirectUrl) window.open(data.redirectUrl)
|
||||
if (data.newVariableValue && callbacks.onNewVariableValue)
|
||||
callbacks.onNewVariableValue(data.newVariableValue)
|
||||
if (data.videoPlayed && callbacks.onVideoPlayed) callbacks.onVideoPlayed()
|
||||
if (data.codeToExecute) data.codeToExecute()
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ export type IframeParams = {
|
||||
|
||||
export type IframeCallbacks = {
|
||||
onNewVariableValue?: (v: Variable) => void
|
||||
onVideoPlayed?: () => void
|
||||
}
|
||||
|
||||
export type PopupParams = {
|
||||
@ -52,10 +51,10 @@ export type Variable = {
|
||||
value: string
|
||||
}
|
||||
|
||||
export type DataFromTypebot = {
|
||||
export type TypebotPostMessageData = {
|
||||
redirectUrl?: string
|
||||
newVariableValue?: Variable
|
||||
videoPlayed?: boolean
|
||||
codeToExecute?: Function
|
||||
}
|
||||
|
||||
export const localStorageKeys = {
|
||||
|
@ -88,26 +88,6 @@ describe('createIframe', () => {
|
||||
expect(v).toBe('varValue')
|
||||
})
|
||||
|
||||
it('should notify when video played', async () => {
|
||||
expect.assertions(1)
|
||||
let hit = false
|
||||
createIframe({
|
||||
url: 'https://typebot.io/typebot-id',
|
||||
onVideoPlayed: () => {
|
||||
hit = true
|
||||
},
|
||||
})
|
||||
window.postMessage(
|
||||
{
|
||||
from: 'typebot',
|
||||
videoPlayed: true,
|
||||
},
|
||||
'*'
|
||||
)
|
||||
await new Promise((r) => setTimeout(r, 1))
|
||||
expect(hit).toBe(true)
|
||||
})
|
||||
|
||||
it("shouldn't execute callbacks if event from other than typebot", async () => {
|
||||
expect.assertions(3)
|
||||
let n, v
|
||||
|
Reference in New Issue
Block a user