Skip to content

Commit a4f03ba

Browse files
committed
feat: added captcha support.
1 parent b6842c6 commit a4f03ba

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

index.html

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<head>
99
<!-- change this to your project name -->
10-
<title>eframe template</title>
10+
<title>Waffle - Template</title>
1111

1212
<!-- config for our rust wasm binary. go to https://trunkrs.dev/assets/#rust for more customization -->
1313
<link data-trunk rel="rust" data-wasm-opt="2" />
@@ -141,6 +141,57 @@
141141
});
142142
}
143143
</script>
144+
145+
<!-- hCaptcha widget integration -->
146+
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
147+
<div id="captcha-container" style="display:none; position:fixed; top:0; left:0; width:100vw; height:100vh; background:rgba(0,0,0,0.7); z-index:9999; align-items:center; justify-content:center;">
148+
<div style="background:#222; padding:32px; border-radius:12px; display:inline-block;">
149+
<div id="hcaptcha-widget" class="h-captcha" data-sitekey="your-hcaptcha-site-key" data-callback="onHCaptchaSuccess"></div>
150+
<button onclick="closeCaptcha()" style="margin-top:16px;">Cancel</button>
151+
</div>
152+
</div>
153+
<script>
154+
// Show/hide captcha overlay via a single JS entrypoint for Rust/WASM
155+
function openCaptcha() {
156+
document.getElementById('captcha-container').style.display = 'flex';
157+
}
158+
function closeCaptcha() {
159+
document.getElementById('captcha-container').style.display = 'none';
160+
}
161+
// Unified JS entrypoint for Rust/WASM with action/message
162+
function JSRust(action, message) {
163+
switch(action) {
164+
case 'openCaptcha':
165+
openCaptcha();
166+
break;
167+
case 'closeCaptcha':
168+
closeCaptcha();
169+
break;
170+
case 'log':
171+
if (message) {
172+
console.log('[JSRust]', message);
173+
}
174+
break;
175+
// Add more actions as needed
176+
default:
177+
console.warn('[JSRust] Unknown action:', action, message);
178+
}
179+
}
180+
// Called by hCaptcha when solved
181+
function onHCaptchaSuccess(token) {
182+
JSRust('closeCaptcha');
183+
if (window.wasm_bindgen && window.wasm_bindgen.pass_captcha_token) {
184+
window.wasm_bindgen.pass_captcha_token(token);
185+
} else if (window.pass_captcha_token) {
186+
window.pass_captcha_token(token);
187+
}
188+
}
189+
// For Rust/WASM: window.JSRust('openCaptcha') or window.JSRust('closeCaptcha')
190+
window.JSRust = JSRust;
191+
window.openCaptcha = openCaptcha;
192+
window.closeCaptcha = closeCaptcha;
193+
</script>
194+
<!-- End hCaptcha integration -->
144195
</body>
145196

146197
</html>

src/erust/uiux/hcaptcha.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// hcaptcha.rs - hCaptcha integration for WASM/egui
2+
use wasm_bindgen::prelude::*;
3+
use wasm_bindgen::JsValue;
4+
use web_sys::js_sys;
5+
use std::cell::RefCell;
6+
7+
thread_local! {
8+
static LAST_TOKEN: RefCell<Option<String>> = RefCell::new(None);
9+
}
10+
11+
#[wasm_bindgen]
12+
pub fn pass_captcha_token(token: String) {
13+
LAST_TOKEN.with(|t| t.replace(Some(token)));
14+
}
15+
16+
pub fn get_captcha_token() -> Option<String> {
17+
LAST_TOKEN.with(|t| t.borrow().clone())
18+
}
19+
20+
pub fn clear_captcha_token() {
21+
LAST_TOKEN.with(|t| t.replace(None));
22+
}
23+
24+
/// Call this from Rust to open the captcha overlay
25+
pub fn open_captcha() {
26+
let _ = js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("JSRust"))
27+
.and_then(|f| if f.is_function() {
28+
let func = js_sys::Function::from(f);
29+
func.call1(&JsValue::NULL, &JsValue::from_str("openCaptcha")).ok();
30+
Ok(())
31+
} else { Ok(()) });
32+
}
33+
34+
/// Call this from Rust to close the captcha overlay
35+
pub fn close_captcha() {
36+
let _ = js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("JSRust"))
37+
.and_then(|f| if f.is_function() {
38+
let func = js_sys::Function::from(f);
39+
func.call1(&JsValue::NULL, &JsValue::from_str("closeCaptcha")).ok();
40+
Ok(())
41+
} else { Ok(()) });
42+
}

src/erust/uiux/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pub mod search;
1+
pub mod search;
2+
pub mod hcaptcha;

0 commit comments

Comments
 (0)