Files
free-games-itchio-list/webapp/src/lib/turnstile.ts
T
poli0981andClaude Opus 5.5 047d918d43 feat: accessibility statement, keyboard/screen-reader/chart/contrast fixes; 4.6.0 (#183) [skip-discuss]
Accessibility pass for the website and the apps (WCAG 2.2 AA target) and the accessibility statement: root ACCESSIBILITY.md + docs/i18n/vi/ACCESSIBILITY.md, the accessibility issue form, a default PR template; focus management, the '/' shortcut switch, chart names and data tables, contrast tokens with a CI check, Windows contrast themes; a docs sync of verified mismatches; version 4.6.0 with the two deps sweeps since 4.5.0 (#176, #182).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 14:16:50 +07:00

48 lines
1.5 KiB
TypeScript

/**
* Cloudflare Turnstile, loaded on demand (in the app only the Suggest page needs it; the
* site CSP allows its script and frame). Explicit rendering, so the widget
* mounts and unmounts with the React component.
*/
interface TurnstileOptions {
sitekey: string
action?: string
theme?: 'auto' | 'light' | 'dark'
/** The widget's language ('en', 'vi', … or 'auto'), so it matches the page around it. */
language?: string
callback: (token: string) => void
'expired-callback'?: () => void
'error-callback'?: () => void
}
interface TurnstileApi {
render(container: HTMLElement, options: TurnstileOptions): string
reset(widgetId: string): void
remove(widgetId: string): void
}
declare global {
interface Window {
turnstile?: TurnstileApi
}
}
const SCRIPT = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'
let loading: Promise<TurnstileApi> | null = null
export function loadTurnstile(): Promise<TurnstileApi> {
if (window.turnstile) return Promise.resolve(window.turnstile)
loading ??= new Promise<TurnstileApi>((resolve, reject) => {
const script = document.createElement('script')
script.src = SCRIPT
script.onload = () => (window.turnstile ? resolve(window.turnstile) : reject(new Error('Turnstile did not load')))
script.onerror = () => {
loading = null
script.remove()
reject(new Error('Turnstile did not load'))
}
document.head.appendChild(script)
})
return loading
}