2
0

🚑 (bot) Share inject custom head code again

This commit is contained in:
Baptiste Arnaud
2023-07-11 10:19:47 +02:00
parent 1beb4bb1b9
commit 3421e4822f
5 changed files with 35 additions and 37 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.0.71", "version": "0.0.72",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -1,33 +0,0 @@
import { isNotDefined } from '@typebot.io/lib/utils'
export const injectCustomHeadCode = (customHeadCode: string) => {
customHeadCode = customHeadCode.replace(
/<script>/g,
`<script>
try {`
)
customHeadCode = customHeadCode.replace(
/<\/script>/g,
`} catch(e) {
console.warn("Error while executing custom head code", e)
}
</script>`
)
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)
})
}

View File

@ -2,9 +2,8 @@
import { initGoogleAnalytics } from '@/lib/gtag' import { initGoogleAnalytics } from '@/lib/gtag'
import { gtmBodyElement } from '@/lib/gtm' import { gtmBodyElement } from '@/lib/gtm'
import { initPixel } from '@/lib/pixel' import { initPixel } from '@/lib/pixel'
import { isNotEmpty } from '@typebot.io/lib/utils' import { injectCustomHeadCode, isNotEmpty } from '@typebot.io/lib/utils'
import { StartPropsToInject } from '@typebot.io/schemas' import { StartPropsToInject } from '@typebot.io/schemas'
import { injectCustomHeadCode } from './injectCustomHeadCode'
export const injectStartProps = async ( export const injectStartProps = async (
startPropsToInject: StartPropsToInject startPropsToInject: StartPropsToInject

View File

@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.0.71", "version": "0.0.72",
"description": "React library to display typebots on your website", "description": "React library to display typebots on your website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@ -286,6 +286,38 @@ 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) => {
customHeadCode = customHeadCode.replace(
/<script>/g,
`<script>
try {`
)
customHeadCode = customHeadCode.replace(
/<\/script>/g,
`} catch(e) {
console.warn("Error while executing custom head code", e)
}
</script>`
)
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)
})
}
export const getAtPath = <T>(obj: T, path: string): unknown => { export const getAtPath = <T>(obj: T, path: string): unknown => {
if (isNotDefined(obj)) return undefined if (isNotDefined(obj)) return undefined
const pathParts = path.split('.') const pathParts = path.split('.')