Skip to content

Commit

Permalink
Merge pull request PHORAX#44 from NMantek/add/turnstilereset
Browse files Browse the repository at this point in the history
Reset turnstile widget after it expires
  • Loading branch information
NMantek authored Oct 4, 2022
2 parents a60136e + eb0d2b1 commit b4bad2a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 228 deletions.
1 change: 1 addition & 0 deletions Resources/Private/TypeScript/definitions/turnstile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare interface RenderParameters {
sitekey: string;
callback?: (token: string) => void;
theme?: string;
action?: string;
}
declare class turnstile {
static render: (
Expand Down
21 changes: 15 additions & 6 deletions Resources/Private/TypeScript/modules/TurnstileForm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class TurnstileForm {
constructor(container: NodeListOf<HTMLFormElement>) {
container.forEach((container) => {
constructor(containerList: NodeListOf<HTMLFormElement>) {
containerList.forEach((container) => {
const captchaDiv = container.querySelector(
'#turnstileDiv'
) as HTMLDivElement;
Expand All @@ -10,18 +10,27 @@ export class TurnstileForm {
return;
}

this.initCaptcha(captchaDiv, sitekey);
const formId = container.getAttribute('id') ?? 'default';

this.initCaptcha(captchaDiv, sitekey, formId);
});
}

private initCaptcha(captchaDiv: HTMLElement, sitekey: string) {
turnstile.render(captchaDiv, {
private initCaptcha(
captchaDiv: HTMLElement,
sitekey: string,
formId: string
) {
const widgetId = turnstile.render(captchaDiv, {
sitekey: sitekey,
callback: function () {
action: formId,
callback: () => {
const captchaInput = captchaDiv.querySelector(
'input'
) as HTMLInputElement;
captchaInput.setAttribute('name', 'formhandler[Turnstile]');

setTimeout(() => turnstile.reset(widgetId), 300000);
},
theme: 'light',
});
Expand Down
Loading

0 comments on commit b4bad2a

Please sign in to comment.