2
0

🐛 (settings) Fix custom head code with noscript injection

This commit is contained in:
Baptiste Arnaud
2022-12-23 16:49:27 +01:00
parent 64cd31cf13
commit 2cdc2b43f5
3 changed files with 34 additions and 8 deletions

View File

@@ -1,13 +1,17 @@
import { LiteBadge } from './LiteBadge'
import { createSignal, onCleanup, onMount, Show } from 'solid-js'
import { getViewerUrl, isDefined, isNotEmpty } from 'utils'
import {
getViewerUrl,
injectCustomHeadCode,
isDefined,
isNotEmpty,
} from 'utils'
import { getInitialChatReplyQuery } from '@/queries/getInitialChatReplyQuery'
import { ConversationContainer } from './ConversationContainer'
import css from '../assets/index.css'
import { InitialChatReply, StartParams } from 'models'
import { setIsMobile } from '@/utils/isMobileSignal'
import { BotContext } from '@/types'
import { injectHeadCode } from '@/utils/injectHeadCode'
export type BotProps = StartParams & {
initialChatReply?: InitialChatReply
@@ -25,9 +29,7 @@ export const Bot = (props: BotProps) => {
if (isDefined(initialChatReplyValue)) {
const customHeadCode =
initialChatReplyValue.typebot.settings.metadata.customHeadCode
if (customHeadCode) {
injectHeadCode(customHeadCode)
}
if (customHeadCode) injectCustomHeadCode(customHeadCode)
} else {
const urlParams = new URLSearchParams(location.search)
const prefilledVariables: { [key: string]: string } = {}

View File

@@ -283,3 +283,23 @@ export const getViewerUrl = (props?: {
export const parseNumberWithCommas = (num: number) =>
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
export const injectCustomHeadCode = (customHeadCode: string) => {
const headCodes = customHeadCode.split('</noscript>')
headCodes.forEach((headCode) => {
const [codeToInject, noScriptContentToInject] = headCode.split('<noscript>')
const fragment = document
.createRange()
.createContextualFragment(codeToInject)
document.head.append(fragment)
if (isNotDefined(noScriptContentToInject)) return
const noScriptElement = document.createElement('noscript')
const noScriptContentFragment = document
.createRange()
.createContextualFragment(noScriptContentToInject)
noScriptElement.append(noScriptContentFragment)
document.head.append(noScriptElement)
})
}