瀏覽代碼

feat: automatic captcha refresh

pplulee 2 月之前
父節點
當前提交
6917d12c3f
共有 1 個文件被更改,包括 34 次插入0 次删除
  1. 34 0
      resources/views/tabler/captcha/js.tpl

+ 34 - 0
resources/views/tabler/captcha/js.tpl

@@ -1,5 +1,12 @@
 {if $public_setting['captcha_provider'] === 'turnstile'}
     <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
+    <script>
+        function refreshCaptcha() {
+            if (window.turnstile) {
+                turnstile.render('#turnstile');
+            }
+        }
+    </script>
 {/if}
 {if $public_setting['captcha_provider'] === 'geetest'}
     <script src="https://static.geetest.com/v4/gt4.js"></script>
@@ -16,10 +23,23 @@
                 geetest_result = geetest.getValidate();
             });
         });
+
+        function refreshCaptcha() {
+            if (window.geetest) {
+                geetest.reset();
+            }
+        }
     </script>
 {/if}
 {if $public_setting['captcha_provider'] === 'hcaptcha'}
     <script src='https://js.hcaptcha.com/1/api.js' async defer></script>
+    <script>
+        function refreshCaptcha() {
+            if (window.hcaptcha) {
+                hcaptcha.reset();
+            }
+        }
+    </script>
 {/if}
 {if $public_setting['captcha_provider'] === 'recaptcha_enterprise'}
     <script src='https://www.recaptcha.net/recaptcha/enterprise.js?onload=initReCAPTCHA&render=explicit' async defer></script>
@@ -29,5 +49,19 @@
                 'sitekey': '{$captcha['recaptcha_enterprise_key_id']}',
             });
         };
+
+        function refreshCaptcha() {
+            if (window.grecaptcha && window.grecaptcha.enterprise) {
+                grecaptcha.enterprise.reset();
+            }
+        }
     </script>
 {/if}
+<script>
+    htmx.on("htmx:afterRequest", function (evt) {
+        let res = JSON.parse(evt.detail.xhr.response);
+        if (res.ret === 0) {
+            refreshCaptcha();
+        }
+    });
+</script>