From a04f8c0f81f55a8b927ce0fad8127db39396f892 Mon Sep 17 00:00:00 2001
From: wxiaoguang <wxiaoguang@gmail.com>
Date: Sun, 7 Jan 2024 23:41:46 +0800
Subject: [PATCH] Move the captcha script loader to the template which really
 needs it (#28718)

It improves the maintainability for the templates.

Now `base/footer.tmpl` doesn't need to depend on "captcha" feature.
---
 templates/base/footer.tmpl       | 12 ------------
 templates/user/auth/captcha.tmpl |  3 +++
 2 files changed, 3 insertions(+), 12 deletions(-)

diff --git a/templates/base/footer.tmpl b/templates/base/footer.tmpl
index 31c669a921..d65a3626a4 100644
--- a/templates/base/footer.tmpl
+++ b/templates/base/footer.tmpl
@@ -13,18 +13,6 @@
 
 	{{template "base/footer_content" .}}
 
-<!-- Third-party libraries -->
-{{if .EnableCaptcha}}
-	{{if eq .CaptchaType "recaptcha"}}
-		<script src='{{URLJoin .RecaptchaURL "api.js"}}'></script>
-	{{end}}
-	{{if eq .CaptchaType "hcaptcha"}}
-		<script src='https://hcaptcha.com/1/api.js'></script>
-	{{end}}
-	{{if eq .CaptchaType "cfturnstile"}}
-		<script src='https://challenges.cloudflare.com/turnstile/v0/api.js'></script>
-	{{end}}
-{{end}}
 	<script src="{{AssetUrlPrefix}}/js/index.js?v={{AssetVersion}}" onerror="alert('Failed to load asset files from ' + this.src + '. Please make sure the asset files can be accessed.')"></script>
 
 	{{template "custom/footer" .}}
diff --git a/templates/user/auth/captcha.tmpl b/templates/user/auth/captcha.tmpl
index c4e58d0134..1c3379e629 100644
--- a/templates/user/auth/captcha.tmpl
+++ b/templates/user/auth/captcha.tmpl
@@ -11,10 +11,12 @@
 	<div class="inline field required">
 		<div id="captcha" data-captcha-type="g-recaptcha" class="g-recaptcha-style" data-sitekey="{{.RecaptchaSitekey}}"></div>
 	</div>
+	<script src='{{URLJoin .RecaptchaURL "api.js"}}'></script>
 {{else if eq .CaptchaType "hcaptcha"}}
 	<div class="inline field required">
 		<div id="captcha" data-captcha-type="h-captcha" class="h-captcha-style" data-sitekey="{{.HcaptchaSitekey}}"></div>
 	</div>
+	<script src='https://hcaptcha.com/1/api.js'></script>
 {{else if eq .CaptchaType "mcaptcha"}}
 	<div class="inline field">
 		<label></label>
@@ -25,4 +27,5 @@
 	<div class="inline field gt-text-center">
 		<div id="captcha" data-captcha-type="cf-turnstile" data-sitekey="{{.CfTurnstileSitekey}}"></div>
 	</div>
+	<script src='https://challenges.cloudflare.com/turnstile/v0/api.js'></script>
 {{end}}{{end}}