Skip to content

Commit

Permalink
adds ability to override GeeTest incoming settings
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi committed Apr 14, 2023
1 parent 893694c commit 5413cae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/hooks/useGeeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ export function useGeeTest(captchaId: string, options: UseGeeTestOptions): UseGe
const [captchaObj, setCaptchaObj] = React.useState<GeeTest>();
const [currentState, setCurrentSate] = React.useState<GeeTestState>('loading');
const [scriptLoaded, setScriptLoaded] = React.useState<boolean>(false);
const { script: staticScript, force = false, ...opts } = options;
const { script: staticScript, overrideWithForce, ...opts } = options;

React.useEffect(() => {
if (typeof window === 'undefined' || scriptLoaded) {
return;
}

if (force) {
if (overrideWithForce) {
// downloads gt4 script and modifies the content
fetch(GT4_JS)
.then((res) => res.text())
.then((text) => {
const script = document.createElement('script');
script.innerHTML = forceChange(opts, text);
script.onload = () => {
setScriptLoaded(true);
};
script.innerHTML = forceChange({ ...opts, overrideWithForce }, text);
script.type = 'text/javascript';
document.head.appendChild(script);
setScriptLoaded(true);
})
.catch((err) => {
console.error('Error when downloading gt4 script', err);
Expand All @@ -40,6 +40,7 @@ export function useGeeTest(captchaId: string, options: UseGeeTestOptions): UseGe
else {
const script = document.createElement('script');
script.src = staticScript || GT4_JS;
script.crossOrigin = 'anonymous';
script.onload = () => {
setScriptLoaded(true);
};
Expand Down Expand Up @@ -94,14 +95,17 @@ export function useGeeTest(captchaId: string, options: UseGeeTestOptions): UseGe
function forceChange(config: UseGeeTestOptions, scriptTxt: string): string {
let modifiedScript = scriptTxt;

const newConfigReg = new RegExp(/var newConfig = camelizeKeys\(newConfig\);/);
const newConfigStr = 'var newConfig = camelizeKeys(newConfig);';

if (config.language) {
modifiedScript.replace(
newConfigReg,
newConfigStr + `newConfig.language = '${config.language}';`,
);
if (config.overrideWithForce) {
Object.keys(config.overrideWithForce).forEach((key) => {
const rowData = config.overrideWithForce[key];
const data = typeof rowData === 'string' ? `'${rowData}'` : rowData;
modifiedScript = modifiedScript.replaceAll(
newConfigStr,
newConfigStr + `newConfig.${key} = ${data};`,
);
});
}

return modifiedScript;
Expand Down
11 changes: 10 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type InitConfig = {
outside?: boolean;
bgColor?: string;
};
force?: boolean;
overrideWithForce?: GeeTestOverrideParams;
apiServers?: string[];
nextWidth?: string;
riskType?: string;
Expand Down Expand Up @@ -125,6 +125,15 @@ export type GeeTestError = {
};
};

export type GeeTestOverrideParams = {
arrow?: string;
show_voice?: boolean;
feedback?: string;
logo?: boolean;
guard?: boolean;
language?: string;
};

declare global {
interface Window {
initGeetest4: typeof initGeetest4;
Expand Down

0 comments on commit 5413cae

Please sign in to comment.