UNPKG

react-google-recaptcha-hook

Version:
49 lines (48 loc) 1.75 kB
import { useCallback as n, useRef as i, useEffect as d } from "react"; const s = (c) => new Promise((e) => { window.___grecaptcha_cfg ??= { fns: [] }, c ? window.grecaptcha?.enterprise ? e(window.grecaptcha.enterprise) : window.___grecaptcha_cfg.fns.push(() => { e(window.grecaptcha.enterprise); }) : window.grecaptcha ? e(window.grecaptcha) : window.___grecaptcha_cfg.fns.push(() => { e(window.grecaptcha); }); }), o = async (c, e, a) => (await s(c)).execute(e, { action: a }), u = async (c) => { (await s(c)).ready(() => { const a = document.querySelector(".grecaptcha-badge"); a && (a.style.visibility = "hidden"); }); }, w = async (c) => { (await s(c)).ready(() => { const a = document.querySelector(".grecaptcha-badge"); a && (a.style.visibility = "visible"); }); }, l = (c, e) => { const a = e?.language ? `&hl=${e.language}` : "", r = e?.enterprise ? "enterprise.js" : "api.js"; return `https://www.${e?.recaptchaNet ? "recaptcha.net" : "google.com"}/recaptcha/${r}?render=${c}${a}`; }, m = (c, e) => { const a = n(() => { if (document.getElementById(c)) return; const t = document.createElement("script"); t.async = !0, t.type = "text/javascript", t.src = l(c, e), t.id = c, document.getElementsByTagName("head")[0].appendChild(t); }, [e, c]), r = n( () => u(!!e?.enterprise), [e?.enterprise] ), h = n( () => w(!!e?.enterprise), [e?.enterprise] ), p = n( (t) => o(!!e?.enterprise, c, t), [e?.enterprise, c] ), g = i(!1); return d(() => { g.current || (g.current = !0, e?.hide && r(), a()); }, [a, r, e]), { executeGoogleReCaptcha: p, hideGoogleReCaptcha: r, showGoogleReCaptcha: h }; }; export { m as useGoogleReCaptcha };