2
0

🚸 (js) Improve setHiddenVariables command

Extend existing variables instead of overwriting everything
This commit is contained in:
Baptiste Arnaud
2023-01-03 10:08:11 +01:00
parent f4615d83cd
commit b105bf8b8e
2 changed files with 9 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{
"name": "typebot-js",
"version": "2.2.15",
"version": "2.2.16",
"main": "dist/index.js",
"unpkg": "dist/index.global.js",
"license": "AGPL-3.0-or-later",

View File

@ -7,18 +7,17 @@ export const setHiddenVariables = (
| HTMLIFrameElement
| undefined
if (!existingIframe) return
const hostUrlParams = new URLSearchParams(document.location.search)
const hostQueryObj: { [key: string]: string } = {}
hostUrlParams.forEach((value, key) => {
hostQueryObj[key] = value
const existingUrl =
existingIframe.getAttribute('data-src') || existingIframe.src
const existingHiddenVariables = new URLSearchParams(existingUrl.split('?')[1])
const existingQueryObj: { [key: string]: string } = {}
existingHiddenVariables.forEach((value, key) => {
existingQueryObj[key] = value
})
const isLoadWhenVisible = existingIframe.hasAttribute('data-src')
const url = (
existingIframe.getAttribute('data-src') || existingIframe.src
).split('?')[0]
const iframeUrl = `${url}${parseQueryParams({
const iframeUrl = `${existingUrl.split('?')[0]}${parseQueryParams({
...existingQueryObj,
...hiddenVariables,
...hostQueryObj,
})}`
existingIframe.setAttribute(isLoadWhenVisible ? 'data-src' : 'src', iframeUrl)
}