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

@@ -2,7 +2,12 @@ import { TypebotViewer } from 'bot-engine'
import { AnswerInput, PublicTypebot, Typebot, VariableWithValue } from 'models' import { AnswerInput, PublicTypebot, Typebot, VariableWithValue } from 'models'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { isDefined, isNotDefined, isNotEmpty } from 'utils' import {
injectCustomHeadCode,
isDefined,
isNotDefined,
isNotEmpty,
} from 'utils'
import { SEO } from './Seo' import { SEO } from './Seo'
import { ErrorPage } from './ErrorPage' import { ErrorPage } from './ErrorPage'
import { createResultQuery, updateResultQuery } from '@/features/results' import { createResultQuery, updateResultQuery } from '@/features/results'
@@ -52,8 +57,7 @@ export const TypebotPage = ({
}) })
setPredefinedVariables(predefinedVariables) setPredefinedVariables(predefinedVariables)
initializeResult().then() initializeResult().then()
if (isDefined(customHeadCode)) if (isDefined(customHeadCode)) injectCustomHeadCode(customHeadCode)
document.head.innerHTML = document.head.innerHTML + customHeadCode
const gtmId = publishedTypebot.settings.metadata.googleTagManagerId const gtmId = publishedTypebot.settings.metadata.googleTagManagerId
if (isNotEmpty(gtmId)) document.body.prepend(gtmBodyElement(gtmId)) if (isNotEmpty(gtmId)) document.body.prepend(gtmBodyElement(gtmId))
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps

View File

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

View File

@@ -283,3 +283,23 @@ export const getViewerUrl = (props?: {
export const parseNumberWithCommas = (num: number) => export const parseNumberWithCommas = (num: number) =>
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') 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)
})
}