Skip to content

Commit e277fd6

Browse files
Fix chat captcha after rebase: restore HCAPTCHA_SITE_KEY global and captcha container
- _Layout.cshtml: emit window.HCAPTCHA_SITE_KEY from HCaptcha:SiteKey config - ChatWidget.vue: destructure captchaContainerEl ref, add hidden container div - chat-module.js: use whenHcaptchaReady() from hcaptcha-form.js for robustness (waits for hcaptcha.js to load before rendering the invisible widget)
1 parent 3e70d70 commit e277fd6

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
window.TRYDOTNET_ORIGIN = @Json.Serialize(Configuration["TryDotNet:Origin"]);
189189
window.BUILD_LABEL = @Json.Serialize(buildLabel);
190190
window.ENABLE_CHAT_WIDGET = @Json.Serialize(!Context.Request.Path.StartsWithSegments("/Identity"));
191+
window.HCAPTCHA_SITE_KEY = @Json.Serialize(Configuration["HCaptcha:SiteKey"]);
191192
</script>
192193
<script src="~/dist/assets/site-shell.js" type="module" asp-append-version="true"></script>
193194
</body>

EssentialCSharp.Web/src/components/ChatWidget.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
isTyping,
1212
chatMessagesEl,
1313
chatInputField,
14+
captchaContainerEl,
1415
openChatDialog,
1516
closeChatDialog,
1617
clearChatHistory,
@@ -189,6 +190,8 @@ const {
189190
Type your question and press Enter or click send. Maximum 500 characters.
190191
</div>
191192
</form>
193+
<!-- Invisible hCaptcha widget attachment point — no visible UI in invisible mode -->
194+
<div ref="captchaContainerEl" style="display:none;" aria-hidden="true"></div>
192195
</div>
193196
</div>
194197
</div>

EssentialCSharp.Web/wwwroot/js/chat-module.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,17 @@ export function useChatWidget() {
144144
async function ensureCaptchaWidget() {
145145
if (!window.HCAPTCHA_SITE_KEY) throw new Error('Captcha is not configured.');
146146
await nextTick();
147-
if (!window.hcaptcha?.render) throw new Error('Captcha script is not ready.');
148147
if (captchaWidgetId !== null) return;
149148

149+
// Wait for hcaptcha.js to load — uses the shared whenHcaptchaReady queue from hcaptcha-form.js
150+
await new Promise((resolve, reject) => {
151+
const timeout = setTimeout(() => reject(new Error('Captcha script is not ready.')), 10000);
152+
window.EssentialCSharp.HCaptcha.whenHcaptchaReady(() => {
153+
clearTimeout(timeout);
154+
resolve();
155+
});
156+
});
157+
150158
captchaWidgetId = window.hcaptcha.render(captchaContainerEl.value, {
151159
sitekey: window.HCAPTCHA_SITE_KEY,
152160
size: 'invisible',

0 commit comments

Comments
 (0)