From b105bf8b8ebde5998fd2bf0bb6c0f66dd7000c1f Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Tue, 3 Jan 2023 10:08:11 +0100 Subject: [PATCH] :children_crossing: (js) Improve setHiddenVariables command Extend existing variables instead of overwriting everything --- packages/typebot-js/package.json | 2 +- .../src/commands/setHiddenVariables.ts | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/typebot-js/package.json b/packages/typebot-js/package.json index cbe9d9fec..c2e878176 100644 --- a/packages/typebot-js/package.json +++ b/packages/typebot-js/package.json @@ -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", diff --git a/packages/typebot-js/src/commands/setHiddenVariables.ts b/packages/typebot-js/src/commands/setHiddenVariables.ts index 7cc3ff9f5..92767b88c 100644 --- a/packages/typebot-js/src/commands/setHiddenVariables.ts +++ b/packages/typebot-js/src/commands/setHiddenVariables.ts @@ -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) }