Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 42x 42x 42x 42x 14x 42x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 1x 5x 2x 2x 2x 2x 5x 5x 4x 4x 2x 2x 6x 6x 1x 5x 5x 5x 5x 5x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x | export type RecaptchaAction = "login" | "CHECKOUT";
export type RecaptchaCheckoutAction = "CHECKOUT" | "CHECKOUT_CHALLENGE";
const SCRIPT_ID = "recaptcha_enterprise_script";
const CHECKBOX_SCRIPT_ID = "recaptcha_enterprise_checkbox_script";
const WAIT_MS = 8000;
type RecaptchaApi = {
ready: (callback: () => void | Promise<void>) => void;
execute: (siteKey: string, options: { action: string }) => Promise<string>;
};
export const getRecaptchaSiteKey = () =>
(process.env.VITE_RECAPTCHA_SITE_KEY || "").trim();
export const getRecaptchaCheckboxSiteKey = () =>
(process.env.VITE_RECAPTCHA_CHECKBOX_SITE_KEY || "").trim();
export function ensureRecaptchaScript() {
const siteKey = getRecaptchaSiteKey();
Iif (!siteKey || typeof document === "undefined") {
return;
}
const src = `https://www.google.com/recaptcha/enterprise.js?render=${encodeURIComponent(
siteKey,
)}`;
const existing = document.getElementById(
SCRIPT_ID,
) as HTMLScriptElement | null;
Iif (existing) {
if (existing.src.includes(siteKey)) {
return;
}
existing.remove();
}
const script = document.createElement("script");
script.id = SCRIPT_ID;
script.src = src;
script.async = true;
document.head.appendChild(script);
}
export function ensureRecaptchaCheckboxScript() {
if (typeof document === "undefined") {
return;
}
if (document.getElementById(CHECKBOX_SCRIPT_ID)) {
return;
}
const script = document.createElement("script");
script.id = CHECKBOX_SCRIPT_ID;
script.src = "https://www.google.com/recaptcha/enterprise.js?render=explicit";
script.async = true;
document.head.appendChild(script);
}
function poll<T>(
read: () => T | undefined | null,
ms: number,
): Promise<T | undefined> {
return new Promise((resolve) => {
const started = Date.now();
const tick = () => {
const value = read();
Eif (value) {
resolve(value);
return;
}
if (Date.now() - started >= ms) {
resolve(undefined);
return;
}
window.setTimeout(tick, 50);
};
tick();
});
}
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
return new Promise((resolve, reject) => {
const timer = window.setTimeout(() => {
reject(new Error("reCAPTCHA timed out"));
}, ms);
promise.then(
(value) => {
window.clearTimeout(timer);
resolve(value);
},
(err) => {
window.clearTimeout(timer);
reject(err);
},
);
});
}
function executeWith(
api: RecaptchaApi,
siteKey: string,
action: RecaptchaAction,
): Promise<string> {
return withTimeout(
new Promise((resolve, reject) => {
api.ready(() => {
api
.execute(siteKey, { action })
.then((token: string) => {
if (token) {
resolve(token);
} else {
reject(new Error("Empty reCAPTCHA token"));
}
})
.catch(reject);
});
}),
WAIT_MS,
);
}
export async function getRecaptchaToken(
action: RecaptchaAction,
): Promise<string> {
const siteKey = getRecaptchaSiteKey();
if (!siteKey) {
return "";
}
ensureRecaptchaScript();
const enterprise = await poll(() => window.grecaptcha?.enterprise, WAIT_MS);
Iif (!enterprise) {
return "";
}
try {
return await executeWith(enterprise, siteKey, action);
} catch {
return "";
}
}
export type RecaptchaCheckboxCallbacks = {
callback: (token: string) => void;
"expired-callback": () => void;
"error-callback": () => void;
};
export async function renderRecaptchaCheckbox(
container: HTMLElement,
callbacks: RecaptchaCheckboxCallbacks,
): Promise<number> {
const siteKey = getRecaptchaCheckboxSiteKey();
if (!siteKey) {
throw new Error("reCAPTCHA checkbox site key is missing");
}
ensureRecaptchaScript();
let enterprise = await poll(() => window.grecaptcha?.enterprise, WAIT_MS);
Iif (!enterprise?.render) {
ensureRecaptchaCheckboxScript();
enterprise = await poll(
() =>
window.grecaptcha?.enterprise?.render
? window.grecaptcha.enterprise
: undefined,
WAIT_MS,
);
}
Iif (!enterprise?.render) {
throw new Error("reCAPTCHA is not available");
}
const api = enterprise;
return new Promise((resolve, reject) => {
api.ready(() => {
try {
resolve(
api.render(container, {
sitekey: siteKey,
action: "CHECKOUT_CHALLENGE",
callback: callbacks.callback,
"expired-callback": callbacks["expired-callback"],
"error-callback": callbacks["error-callback"],
}),
);
} catch (err) {
reject(err);
}
});
});
}
export function resetRecaptchaCheckbox(widgetId: number) {
window.grecaptcha?.enterprise?.reset?.(widgetId);
}
export function withRecaptchaToken(
action: RecaptchaAction,
onToken: (token: string) => void,
) {
getRecaptchaToken(action)
.then((token) => {
if (token) {
onToken(token);
}
})
.catch((err) => {
console.error("reCAPTCHA execute failed:", err);
});
}
|