|
| 1 | +//! Browser API — commands for the embedded browser feature. |
| 2 | +
|
| 3 | +use serde::Deserialize; |
| 4 | +use tauri::Manager; |
| 5 | + |
| 6 | +#[derive(Debug, Deserialize)] |
| 7 | +#[serde(rename_all = "camelCase")] |
| 8 | +pub struct WebviewEvalRequest { |
| 9 | + pub label: String, |
| 10 | + pub script: String, |
| 11 | +} |
| 12 | + |
| 13 | +#[tauri::command] |
| 14 | +pub async fn browser_webview_eval( |
| 15 | + app: tauri::AppHandle, |
| 16 | + request: WebviewEvalRequest, |
| 17 | +) -> Result<(), String> { |
| 18 | + let webview = app |
| 19 | + .get_webview(&request.label) |
| 20 | + .ok_or_else(|| format!("Webview not found: {}", request.label))?; |
| 21 | + |
| 22 | + webview |
| 23 | + .eval(&request.script) |
| 24 | + .map_err(|e| format!("eval failed: {e}")) |
| 25 | +} |
| 26 | + |
| 27 | +// #region agent log |
| 28 | +#[derive(Debug, Deserialize)] |
| 29 | +#[serde(rename_all = "camelCase")] |
| 30 | +pub struct WebviewLabelRequest { |
| 31 | + pub label: String, |
| 32 | +} |
| 33 | + |
| 34 | +/// Pull debug logs from the injected inspector script. |
| 35 | +/// The script stores logs in `window.__bitfun_debug_logs`. |
| 36 | +/// We eval a script that writes them into URL hash, then read the URL. |
| 37 | +#[tauri::command] |
| 38 | +pub async fn browser_pull_debug_logs( |
| 39 | + app: tauri::AppHandle, |
| 40 | + request: WebviewLabelRequest, |
| 41 | +) -> Result<String, String> { |
| 42 | + let webview = app |
| 43 | + .get_webview(&request.label) |
| 44 | + .ok_or_else(|| format!("Webview not found: {}", request.label))?; |
| 45 | + |
| 46 | + webview |
| 47 | + .eval( |
| 48 | + r#"(function(){ |
| 49 | + var logs = window.__bitfun_debug_logs || []; |
| 50 | + window.__bitfun_debug_logs = []; |
| 51 | + try { |
| 52 | + var hash = '__BFDEBUG__' + encodeURIComponent(JSON.stringify(logs)); |
| 53 | + history.replaceState(null, '', '#' + hash); |
| 54 | + } catch(e) { |
| 55 | + history.replaceState(null, '', '#__BFDEBUG__ERR_' + encodeURIComponent(String(e))); |
| 56 | + } |
| 57 | + })()"#, |
| 58 | + ) |
| 59 | + .map_err(|e| format!("eval failed: {e}"))?; |
| 60 | + |
| 61 | + tokio::time::sleep(std::time::Duration::from_millis(300)).await; |
| 62 | + |
| 63 | + let url = webview.url().map_err(|e| format!("url failed: {e}"))?; |
| 64 | + let fragment = url.fragment().unwrap_or(""); |
| 65 | + |
| 66 | + if fragment.starts_with("__BFDEBUG__") { |
| 67 | + let encoded = &fragment[11..]; |
| 68 | + let decoded = urlencoding::decode(encoded) |
| 69 | + .map(|s| s.into_owned()) |
| 70 | + .unwrap_or_else(|_| format!("decode_error:{}", encoded)); |
| 71 | + |
| 72 | + webview |
| 73 | + .eval("try { history.replaceState(null, '', location.pathname + location.search); } catch(e) {}") |
| 74 | + .ok(); |
| 75 | + |
| 76 | + Ok(decoded) |
| 77 | + } else { |
| 78 | + Ok(format!("no_debug_marker_in_fragment:{}", &fragment.chars().take(100).collect::<String>())) |
| 79 | + } |
| 80 | +} |
| 81 | +// #endregion |
0 commit comments