2
0

Remove invalid js in meta head code

This commit is contained in:
Baptiste Arnaud
2023-07-15 16:39:45 +02:00
parent d3fb31b43a
commit 521cb50782

View File

@ -498,6 +498,7 @@ const injectTryCatch = (headCode: string) => {
const wrappedTag = tag.replace(
/(<script\b[^>]*>)([\s\S]*?)(<\/script>)/gi,
function (_, openingTag, content, closingTag) {
if (!isValidJsSyntax(content)) return ''
return `${openingTag}
try {
${content}
@ -512,3 +513,12 @@ ${closingTag}`
}
return headCode
}
const isValidJsSyntax = (snippet: string): boolean => {
try {
new Function(snippet)
return true
} catch (err) {
return false
}
}