-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
script.ts
110 lines (98 loc) 路 3.12 KB
/
script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { TurnstileRenderOptions } from '../types'
import { defineNuxtPlugin, useRuntimeConfig, useHead, ref, isVue2, watch } from '#imports'
const configure = [
'window.loadTurnstile = new Promise(resolve => {',
' window.onloadTurnstileCallback = function () {',
' resolve();',
' delete window.onloadTurnstileCallback;',
' delete window.loadTurnstile;',
' }',
'})',
]
.map(l => l.trim())
.join(' ')
const turnstileScript = {
src: 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback',
async: true,
defer: true,
}
type TurnstileInjection = {
loadTurnstile: () => Promise<void>
render(element: string | HTMLElement, options: TurnstileRenderOptions): Promise<string>
reset(id: string): Promise<any>
remove(id: string): Promise<any>
}
export default defineNuxtPlugin(nuxtApp => {
const addTurnstileScript = ref(false)
const config = useRuntimeConfig()
const turnstile = {
loadTurnstile: async () => {
addTurnstileScript.value = true
if (import.meta.server) return
;(await (window as any).loadTurnstile) as Promise<void>
},
async render(element, options) {
if (import.meta.server) return
await this.loadTurnstile()
return (window as any).turnstile.render(element, {
sitekey: config.public.turnstile.siteKey,
...options,
})
},
async reset(element) {
if (import.meta.server) return
await this.loadTurnstile()
return (window as any).turnstile.reset(element)
},
async remove(element) {
if (import.meta.server) return
if (addTurnstileScript.value) {
return (window as any).turnstile.remove(element)
} else {
console.warn('Cannot remove a Turnstile widget without enabling Turnstile.')
}
},
} satisfies TurnstileInjection
if (isVue2) {
// @ts-expect-error untyped nuxt2Context - fix in bridge-schema
const app = nuxtApp.nuxt2Context.app
const originalHead = app.head
app.head = function () {
const head =
(typeof originalHead === 'function' ? originalHead.call(this) : originalHead) || {}
head.__dangerouslyDisableSanitizersByTagID = head.__dangerouslyDisableSanitizersByTagID || {}
head.__dangerouslyDisableSanitizersByTagID['cf-configure'] = ['innerHTML']
head.script = head.script || []
head.script.push(
...[
{ hid: 'cf-configure', innerHTML: configure },
addTurnstileScript.value && turnstileScript,
].filter(Boolean)
)
return head
}
} else {
const script = () =>
[{ children: configure }, addTurnstileScript.value && turnstileScript].filter(
(s): s is typeof turnstileScript => !!s
)
const head = useHead({ script: script() })
if (!addTurnstileScript.value && head) {
const unsubscribe = watch(addTurnstileScript, () => {
unsubscribe()
head.patch({ script: script() })
})
}
}
return {
provide: {
turnstile,
},
}
})
// TODO: fix this issue upstream in nuxt/module-builder
declare module '#app' {
interface NuxtApp {
$turnstile: TurnstileInjection
}
}