1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00
jfa-go/ts/modules/captcha.ts

82 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-12-23 18:20:09 +00:00
import { _get, _post } from "./common.js";
export class Captcha {
2023-12-23 19:56:21 +00:00
isPWR = false;
2023-12-23 18:20:09 +00:00
enabled = true;
verified = false;
captchaID = "";
input = document.getElementById("captcha-input") as HTMLInputElement;
checkbox = document.getElementById("captcha-success") as HTMLSpanElement;
previous = "";
reCAPTCHA = false;
code = "";
get value(): string { return this.input.value; }
hasChanged = (): boolean => { return this.value != this.previous; }
baseValidatorWrapper = (_baseValidator: (oncomplete: (valid: boolean) => void, captchaValid: boolean) => void) => {
return (oncomplete: (valid: boolean) => void): void => {
if (this.enabled && !this.reCAPTCHA && this.hasChanged()) {
this.previous = this.value;
this.verify(() => {
_baseValidator(oncomplete, this.verified);
});
} else {
_baseValidator(oncomplete, this.verified);
}
};
};
2023-12-23 19:56:21 +00:00
verify = (callback: () => void) => _post("/captcha/verify/" + this.code + "/" + this.captchaID + "/" + this.input.value + (this.isPWR ? "?pwr=true" : ""), null, (req: XMLHttpRequest) => {
2023-12-23 18:20:09 +00:00
if (req.readyState == 4) {
if (req.status == 204) {
this.checkbox.innerHTML = `<i class="ri-check-line"></i>`;
this.checkbox.classList.add("~positive");
this.checkbox.classList.remove("~critical");
this.verified = true;
} else {
this.checkbox.innerHTML = `<i class="ri-close-line"></i>`;
this.checkbox.classList.add("~critical");
this.checkbox.classList.remove("~positive");
this.verified = false;
}
callback();
}
});
2023-12-23 19:56:21 +00:00
generate = () => _get("/captcha/gen/"+this.code+(this.isPWR ? "?pwr=true" : ""), null, (req: XMLHttpRequest) => {
2023-12-23 18:20:09 +00:00
if (req.readyState == 4) {
if (req.status == 200) {
2023-12-23 19:56:21 +00:00
this.captchaID = this.isPWR ? this.code : req.response["id"];
// the Math.random() appearance below is used for PWRs, since they don't have a unique captchaID. The parameter is ignored by the server, but tells the browser to reload the image.
2023-12-23 18:20:09 +00:00
document.getElementById("captcha-img").innerHTML = `
2023-12-23 19:56:21 +00:00
<img class="w-100" src="${window.location.toString().substring(0, window.location.toString().lastIndexOf("/invite"))}/captcha/img/${this.code}/${this.isPWR ? Math.random() : this.captchaID}${this.isPWR ? "?pwr=true" : ""}"></img>
2023-12-23 18:20:09 +00:00
`;
this.input.value = "";
}
}
});
2023-12-23 19:56:21 +00:00
constructor(code: string, enabled: boolean, reCAPTCHA: boolean, isPWR: boolean) {
2023-12-23 18:20:09 +00:00
this.code = code;
this.enabled = enabled;
this.reCAPTCHA = reCAPTCHA;
2023-12-23 19:56:21 +00:00
this.isPWR = isPWR;
2023-12-23 18:20:09 +00:00
}
}
2023-12-23 19:56:21 +00:00
export interface GreCAPTCHA {
render: (container: HTMLDivElement, parameters: {
sitekey?: string,
theme?: string,
size?: string,
tabindex?: number,
"callback"?: () => void,
"expired-callback"?: () => void,
"error-callback"?: () => void
}) => void;
getResponse: (opt_widget_id?: HTMLDivElement) => string;
}