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", "name": "typebot-js",
"version": "2.2.15", "version": "2.2.16",
"main": "dist/index.js", "main": "dist/index.js",
"unpkg": "dist/index.global.js", "unpkg": "dist/index.global.js",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",

View File

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