@gahojin-inc/react-google-recaptcha 2025.8.0 → 2025.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context.d.mts +2 -2
- package/dist/index.mjs +87 -54
- package/dist/index.mjs.map +1 -1
- package/dist/types.d.mts +34 -17
- package/dist/utils.d.mts +4 -0
- package/package.json +1 -1
package/dist/context.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Context, type JSX } from "react";
|
|
2
2
|
import type { GoogleReCaptchaContextType, GoogleReCaptchaProviderProps } from "./types.mjs";
|
|
3
|
-
declare const GoogleReCaptchaContext: Context<GoogleReCaptchaContextType
|
|
4
|
-
declare const GoogleReCaptchaProvider: ({ siteKey, language,
|
|
3
|
+
declare const GoogleReCaptchaContext: Context<GoogleReCaptchaContextType>;
|
|
4
|
+
declare const GoogleReCaptchaProvider: ({ siteKey, language, useRecaptchaNet, useEnterprise, container, badge, theme, children }: GoogleReCaptchaProviderProps) => JSX.Element;
|
|
5
5
|
export { GoogleReCaptchaProvider, GoogleReCaptchaContext };
|
|
6
6
|
export type { GoogleReCaptchaContextType, GoogleReCaptchaProviderProps };
|
package/dist/index.mjs
CHANGED
|
@@ -1,87 +1,120 @@
|
|
|
1
|
-
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import { jsx } from "react/jsx-runtime";
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useId, useMemo, useState } from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
3
|
|
|
4
|
+
//#region src/utils.ts
|
|
5
|
+
const getRecaptchaScriptSrc = (render, language, useRecaptchaNet, useEnterprise) => {
|
|
6
|
+
const hostName = useRecaptchaNet ? "recaptcha.net" : "google.com";
|
|
7
|
+
const script = useEnterprise ? "enterprise.js" : "api.js";
|
|
8
|
+
return `https://${hostName}/recaptcha/${script}?render=${render}${language ? `&hl=${language}` : ""}`;
|
|
9
|
+
};
|
|
10
|
+
const injectScriptTag = (scriptId, language, useRecaptchaNet, useEnterprise, onLoad) => {
|
|
11
|
+
const script = document.createElement("script");
|
|
12
|
+
script.type = "text/javascript";
|
|
13
|
+
script.id = scriptId;
|
|
14
|
+
script.src = getRecaptchaScriptSrc("explicit", language, useRecaptchaNet, useEnterprise);
|
|
15
|
+
script.async = true;
|
|
16
|
+
script.defer = true;
|
|
17
|
+
script.onload = onLoad;
|
|
18
|
+
const head = document.getElementsByTagName("head")[0];
|
|
19
|
+
head.appendChild(script);
|
|
20
|
+
return script;
|
|
21
|
+
};
|
|
22
|
+
const removeScriptTag = () => {
|
|
23
|
+
removeRecaptchBadge();
|
|
24
|
+
const script = document.querySelector("script[src^=\"https://www.gstatic.com/recaptcha/releases\"]");
|
|
25
|
+
if (script) script.remove();
|
|
26
|
+
};
|
|
27
|
+
const removeRecaptchBadge = () => {
|
|
28
|
+
const nodeBadge = document.querySelector(".grecaptcha-badge");
|
|
29
|
+
if (nodeBadge?.parentNode) document.body.removeChild(nodeBadge.parentNode);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
4
33
|
//#region src/context.tsx
|
|
5
|
-
const GoogleReCaptchaContext = createContext(
|
|
6
|
-
|
|
7
|
-
|
|
34
|
+
const GoogleReCaptchaContext = createContext({
|
|
35
|
+
isLoading: true,
|
|
36
|
+
error: null,
|
|
37
|
+
execute: () => Promise.reject("useGoogleRecaptcha must be used within an GoogleReCaptchaProvider"),
|
|
38
|
+
reset: () => []
|
|
39
|
+
});
|
|
40
|
+
const GoogleReCaptchaProvider = ({ siteKey, language, useRecaptchaNet, useEnterprise, container, badge, theme, children }) => {
|
|
41
|
+
const id = useId();
|
|
42
|
+
const containerId = useMemo(() => container ? container : `${id}-container`, [id, container]);
|
|
43
|
+
const [state, setState] = useState({
|
|
44
|
+
isLoading: true,
|
|
45
|
+
widgetId: siteKey
|
|
46
|
+
});
|
|
8
47
|
const onLoad = useCallback(() => {
|
|
9
|
-
|
|
48
|
+
const grecaptcha = window.grecaptcha?.enterprise ?? window.grecaptcha;
|
|
49
|
+
if (!grecaptcha) {
|
|
10
50
|
setState({
|
|
11
51
|
isLoading: false,
|
|
12
|
-
|
|
52
|
+
widgetId: siteKey,
|
|
53
|
+
error: /* @__PURE__ */ new Error("ReCaptcha is not available")
|
|
13
54
|
});
|
|
14
55
|
return;
|
|
15
56
|
}
|
|
16
|
-
|
|
17
|
-
|
|
57
|
+
grecaptcha.ready(() => {
|
|
58
|
+
const widgetId = grecaptcha.render(containerId, {
|
|
59
|
+
sitekey: siteKey,
|
|
60
|
+
badge,
|
|
61
|
+
theme,
|
|
62
|
+
size: "invisible"
|
|
63
|
+
});
|
|
18
64
|
setState({
|
|
19
|
-
|
|
65
|
+
widgetId,
|
|
66
|
+
grecaptcha,
|
|
20
67
|
error: null,
|
|
21
68
|
isLoading: false
|
|
22
69
|
});
|
|
23
70
|
});
|
|
24
|
-
}, [
|
|
71
|
+
}, [
|
|
72
|
+
siteKey,
|
|
73
|
+
containerId,
|
|
74
|
+
badge,
|
|
75
|
+
theme
|
|
76
|
+
]);
|
|
25
77
|
const execute = useCallback(async (action) => {
|
|
26
78
|
const grecaptcha = state?.grecaptcha;
|
|
27
|
-
if (grecaptcha?.execute) return await grecaptcha.execute(
|
|
28
|
-
throw new Error("
|
|
29
|
-
}, [
|
|
79
|
+
if (grecaptcha?.execute) return await grecaptcha.execute(state.widgetId, action ? { action } : void 0);
|
|
80
|
+
throw new Error("ReCaptcha is not available");
|
|
81
|
+
}, [state]);
|
|
82
|
+
const reset = useCallback(() => {
|
|
83
|
+
state?.grecaptcha?.reset(state.widgetId);
|
|
84
|
+
}, [state]);
|
|
30
85
|
useEffect(() => {
|
|
31
|
-
|
|
86
|
+
const removeScript = injectScriptTag(id, language, useRecaptchaNet, useEnterprise, onLoad);
|
|
32
87
|
return () => {
|
|
33
|
-
|
|
88
|
+
removeScript?.remove();
|
|
89
|
+
removeScriptTag();
|
|
34
90
|
};
|
|
35
91
|
}, [
|
|
36
|
-
|
|
37
|
-
siteKey,
|
|
92
|
+
id,
|
|
38
93
|
language,
|
|
39
|
-
|
|
94
|
+
useRecaptchaNet,
|
|
95
|
+
useEnterprise,
|
|
96
|
+
onLoad
|
|
40
97
|
]);
|
|
41
98
|
const value = useMemo(() => ({
|
|
42
99
|
isLoading: state.isLoading,
|
|
43
|
-
error: state.error,
|
|
44
|
-
execute
|
|
45
|
-
|
|
46
|
-
|
|
100
|
+
error: state.error ?? null,
|
|
101
|
+
execute,
|
|
102
|
+
reset
|
|
103
|
+
}), [
|
|
104
|
+
state,
|
|
105
|
+
execute,
|
|
106
|
+
reset
|
|
107
|
+
]);
|
|
108
|
+
return /* @__PURE__ */ jsxs(GoogleReCaptchaContext.Provider, {
|
|
47
109
|
value,
|
|
48
|
-
children
|
|
110
|
+
children: [children, container ? null : /* @__PURE__ */ jsx("div", { id: `${id}-container` })]
|
|
49
111
|
});
|
|
50
112
|
};
|
|
51
|
-
const isScriptInjected = (siteKey) => {
|
|
52
|
-
return !!document.getElementById(siteKey);
|
|
53
|
-
};
|
|
54
|
-
const injectScriptTag = (siteKey, language, async, defer, nonce, trustedtypes, onLoad) => {
|
|
55
|
-
const script = document.createElement("script");
|
|
56
|
-
script.id = siteKey;
|
|
57
|
-
script.src = `https://www.google.com/recaptcha/api.js?render=${siteKey}${language ? "&hl=${language}" : ""}${trustedtypes ? "&trustedtypes=true" : ""}`;
|
|
58
|
-
script.async = async;
|
|
59
|
-
script.defer = defer;
|
|
60
|
-
if (nonce) script.nonce = nonce;
|
|
61
|
-
script.type = "text/javascript";
|
|
62
|
-
script.onload = onLoad;
|
|
63
|
-
const head = document.getElementsByTagName("head")[0];
|
|
64
|
-
head.appendChild(script);
|
|
65
|
-
};
|
|
66
|
-
const removeScriptTag = (siteKey) => {
|
|
67
|
-
if (!document) return;
|
|
68
|
-
removeRecaptchBadge();
|
|
69
|
-
let script = document.getElementById(siteKey);
|
|
70
|
-
if (script) script.remove();
|
|
71
|
-
script = document.querySelector("script[src^=\"https://www.gstatic.com/recaptcha/releases\"]");
|
|
72
|
-
if (script) script.remove();
|
|
73
|
-
};
|
|
74
|
-
const removeRecaptchBadge = () => {
|
|
75
|
-
const nodeBadge = document.querySelector(".grecaptcha-badge");
|
|
76
|
-
if (nodeBadge && nodeBadge.parentNode) document.body.removeChild(nodeBadge.parentNode);
|
|
77
|
-
};
|
|
78
113
|
|
|
79
114
|
//#endregion
|
|
80
115
|
//#region src/hooks.ts
|
|
81
116
|
const useGoogleRecaptcha = () => {
|
|
82
|
-
|
|
83
|
-
if (context === void 0) throw new Error("useGoogleRecaptcha must be used within an GoogleReCaptchaProvider");
|
|
84
|
-
return context;
|
|
117
|
+
return useContext(GoogleReCaptchaContext);
|
|
85
118
|
};
|
|
86
119
|
|
|
87
120
|
//#endregion
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["GoogleReCaptchaContext: Context<GoogleReCaptchaContextType
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["GoogleReCaptchaContext: Context<GoogleReCaptchaContextType>","value: GoogleReCaptchaContextType"],"sources":["../src/utils.ts","../src/context.tsx","../src/hooks.ts"],"sourcesContent":["export const getRecaptchaScriptSrc = (\n render: string,\n language: string | null | undefined,\n useRecaptchaNet: boolean | undefined,\n useEnterprise: boolean | undefined,\n): string => {\n const hostName = useRecaptchaNet ? 'recaptcha.net' : 'google.com'\n const script = useEnterprise ? 'enterprise.js' : 'api.js'\n\n return `https://${hostName}/recaptcha/${script}?render=${render}${language ? `&hl=${language}` : ''}`\n}\n\nexport const injectScriptTag = (\n scriptId: string,\n language: string | null | undefined,\n useRecaptchaNet: boolean | undefined,\n useEnterprise: boolean | undefined,\n onLoad: () => void,\n): HTMLScriptElement => {\n const script = document.createElement('script')\n script.type = 'text/javascript'\n script.id = scriptId\n script.src = getRecaptchaScriptSrc('explicit', language, useRecaptchaNet, useEnterprise)\n script.async = true\n script.defer = true\n script.onload = onLoad\n\n const head = document.getElementsByTagName('head')[0]\n head.appendChild(script)\n return script\n}\n\nexport const removeScriptTag = (): void => {\n removeRecaptchBadge()\n\n const script = document.querySelector('script[src^=\"https://www.gstatic.com/recaptcha/releases\"]')\n if (script) {\n script.remove()\n }\n}\n\nexport const removeRecaptchBadge = (): void => {\n const nodeBadge = document.querySelector('.grecaptcha-badge')\n if (nodeBadge?.parentNode) {\n document.body.removeChild(nodeBadge.parentNode)\n }\n}\n","import { type Context, createContext, type JSX, useCallback, useEffect, useId, useMemo, useState } from 'react'\nimport type { GoogleReCaptchaContextType, GoogleReCaptchaProviderProps, Grecaptcha } from './types'\nimport { injectScriptTag, removeScriptTag } from './utils.ts'\n\nconst GoogleReCaptchaContext: Context<GoogleReCaptchaContextType> = createContext<GoogleReCaptchaContextType>({\n isLoading: true,\n error: null,\n execute: () => Promise.reject('useGoogleRecaptcha must be used within an GoogleReCaptchaProvider'),\n reset: () => [],\n})\n\ntype State = {\n isLoading: boolean\n error?: Error | null\n widgetId: string\n grecaptcha?: Grecaptcha | null\n}\n\nconst GoogleReCaptchaProvider = ({\n siteKey,\n language,\n useRecaptchaNet,\n useEnterprise,\n container,\n badge,\n theme,\n children,\n}: GoogleReCaptchaProviderProps): JSX.Element => {\n const id = useId()\n const containerId = useMemo(() => (container ? container : `${id}-container`), [id, container])\n const [state, setState] = useState<State>({ isLoading: true, widgetId: siteKey })\n\n // ReCaptcha初期化\n const onLoad = useCallback(() => {\n const grecaptcha = window.grecaptcha?.enterprise ?? window.grecaptcha\n if (!grecaptcha) {\n setState({\n isLoading: false,\n widgetId: siteKey,\n error: new Error('ReCaptcha is not available'),\n })\n return\n }\n grecaptcha.ready(() => {\n const widgetId = grecaptcha.render(containerId, {\n sitekey: siteKey,\n badge,\n theme,\n size: 'invisible',\n })\n setState({\n widgetId,\n grecaptcha,\n error: null,\n isLoading: false,\n })\n })\n }, [siteKey, containerId, badge, theme])\n\n const execute = useCallback(\n async (action?: string) => {\n const grecaptcha = state?.grecaptcha\n if (grecaptcha?.execute) {\n return await grecaptcha.execute(state.widgetId, action ? { action } : undefined)\n }\n throw new Error('ReCaptcha is not available')\n },\n [state],\n )\n\n const reset = useCallback(() => {\n state?.grecaptcha?.reset(state.widgetId)\n }, [state])\n\n // scriptタグを追加/削除する\n useEffect(() => {\n const removeScript = injectScriptTag(id, language, useRecaptchaNet, useEnterprise, onLoad)\n return () => {\n removeScript?.remove()\n removeScriptTag()\n }\n }, [id, language, useRecaptchaNet, useEnterprise, onLoad])\n\n const value: GoogleReCaptchaContextType = useMemo(\n () => ({\n isLoading: state.isLoading,\n error: state.error ?? null,\n execute,\n reset,\n }),\n [state, execute, reset],\n )\n\n return (\n <GoogleReCaptchaContext.Provider value={value}>\n {children}\n {container ? null : <div id={`${id}-container`} />}\n </GoogleReCaptchaContext.Provider>\n )\n}\n\nexport { GoogleReCaptchaProvider, GoogleReCaptchaContext }\nexport type { GoogleReCaptchaContextType, GoogleReCaptchaProviderProps }\n","import { useContext } from 'react'\nimport { GoogleReCaptchaContext, type GoogleReCaptchaContextType } from './context'\n\nconst useGoogleRecaptcha = (): GoogleReCaptchaContextType => {\n return useContext(GoogleReCaptchaContext)\n}\n\nexport { useGoogleRecaptcha }\n"],"mappings":";;;;AAAA,MAAa,yBACX,QACA,UACA,iBACA,kBACW;CACX,MAAM,WAAW,kBAAkB,kBAAkB;CACrD,MAAM,SAAS,gBAAgB,kBAAkB;AAEjD,QAAO,WAAW,SAAS,aAAa,OAAO,UAAU,SAAS,WAAW,OAAO,aAAa;AAClG;AAED,MAAa,mBACX,UACA,UACA,iBACA,eACA,WACsB;CACtB,MAAM,SAAS,SAAS,cAAc;AACtC,QAAO,OAAO;AACd,QAAO,KAAK;AACZ,QAAO,MAAM,sBAAsB,YAAY,UAAU,iBAAiB;AAC1E,QAAO,QAAQ;AACf,QAAO,QAAQ;AACf,QAAO,SAAS;CAEhB,MAAM,OAAO,SAAS,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;EACd,MAAM,eAAe,gBAAgB,IAAI,UAAU,iBAAiB,eAAe;AACnF,eAAa;AACX,iBAAc;AACd;EACD;CACF,GAAE;EAAC;EAAI;EAAU;EAAiB;EAAe;EAAO;CAEzD,MAAMC,QAAoC,eACjC;EACL,WAAW,MAAM;EACjB,OAAO,MAAM,SAAS;EACtB;EACA;EACD,GACD;EAAC;EAAO;EAAS;EAAM;AAGzB,QACE,qBAAC,uBAAuB;EAAgB;aACrC,UACA,YAAY,OAAO,oBAAC,SAAI,IAAI,GAAG,GAAG;;AAGxC;;;;AChGD,MAAM,2BAAuD;AAC3D,QAAO,WAAW;AACnB"}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,23 +1,40 @@
|
|
|
1
1
|
import type { PropsWithChildren } from "react";
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
export type Theme = "light" | "dark";
|
|
3
|
+
export type Type = "image" | "audio";
|
|
4
|
+
export type Size = "normal" | "compact" | "invisible";
|
|
5
|
+
export type Badge = "bottomright" | "bottomleft" | "inline";
|
|
6
|
+
export type Parameters = {
|
|
7
|
+
sitekey: string;
|
|
8
|
+
badge?: Badge;
|
|
9
|
+
size?: Size;
|
|
10
|
+
theme?: Theme;
|
|
7
11
|
};
|
|
8
|
-
type GoogleReCaptchaProviderProps = PropsWithChildren<{
|
|
9
|
-
siteKey: string;
|
|
10
|
-
language?: string;
|
|
11
|
-
|
|
12
|
+
export type GoogleReCaptchaProviderProps = PropsWithChildren<{
|
|
13
|
+
readonly siteKey: string;
|
|
14
|
+
readonly language?: string | null;
|
|
15
|
+
readonly useRecaptchaNet?: boolean;
|
|
16
|
+
readonly useEnterprise?: boolean;
|
|
17
|
+
readonly container?: string | HTMLElement | null;
|
|
18
|
+
readonly badge?: Badge;
|
|
19
|
+
readonly theme?: Theme;
|
|
12
20
|
}>;
|
|
13
|
-
type
|
|
14
|
-
|
|
15
|
-
action?: string;
|
|
16
|
-
}) => Promise<string>;
|
|
21
|
+
export type Action = {
|
|
22
|
+
action: string;
|
|
17
23
|
};
|
|
18
|
-
type
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
export type Grecaptcha = {
|
|
25
|
+
ready: (onReady: () => void) => void;
|
|
26
|
+
render: (container: string | HTMLElement, params: Parameters) => string;
|
|
27
|
+
execute: (widgetId: string, params?: Action) => Promise<string>;
|
|
28
|
+
reset: (widgetId: string) => void;
|
|
29
|
+
};
|
|
30
|
+
export type GoogleReCaptchaContextType = {
|
|
31
|
+
readonly isLoading: boolean;
|
|
32
|
+
readonly error: Error | null;
|
|
21
33
|
execute: (action?: string) => Promise<string>;
|
|
34
|
+
reset: () => void;
|
|
22
35
|
};
|
|
23
|
-
|
|
36
|
+
declare global {
|
|
37
|
+
var grecaptcha: Grecaptcha & {
|
|
38
|
+
enterprise: Grecaptcha;
|
|
39
|
+
};
|
|
40
|
+
}
|
package/dist/utils.d.mts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const getRecaptchaScriptSrc: (render: string, language: string | null | undefined, useRecaptchaNet: boolean | undefined, useEnterprise: boolean | undefined) => string;
|
|
2
|
+
export declare const injectScriptTag: (scriptId: string, language: string | null | undefined, useRecaptchaNet: boolean | undefined, useEnterprise: boolean | undefined, onLoad: () => void) => HTMLScriptElement;
|
|
3
|
+
export declare const removeScriptTag: () => void;
|
|
4
|
+
export declare const removeRecaptchBadge: () => void;
|