| 43 | } |
| 44 | |
| 45 | fn process_console_events( |
| 46 | events: &[serde_json::Value], |
| 47 | type_filter: &[String], |
| 48 | ) -> Vec<serde_json::Value> { |
| 49 | // Match case-insensitively and accept the common "warn" shorthand for the |
| 50 | // CDP type "warning" (mirrors `console.warn`). CDP console types are already |
| 51 | // lowercase, so this mainly hardens against odd-cased / shorthand input. |
| 52 | let filter_set: Option<std::collections::HashSet<String>> = if type_filter.is_empty() { |
| 53 | None |
| 54 | } else { |
| 55 | Some( |
| 56 | type_filter |
| 57 | .iter() |
| 58 | .map(|s| { |
| 59 | let s = s.to_lowercase(); |
| 60 | if s == "warn" { |
| 61 | "warning".to_string() |
| 62 | } else { |
| 63 | s |
| 64 | } |
| 65 | }) |
| 66 | .collect(), |
| 67 | ) |
| 68 | }; |
| 69 | |
| 70 | let mut messages = Vec::new(); |
| 71 | |
| 72 | for event in events { |
| 73 | let method = event["method"].as_str().unwrap_or(""); |
| 74 | let params = &event["params"]; |
| 75 | |
| 76 | match method { |
| 77 | "Runtime.consoleAPICalled" => { |
| 78 | let msg_type = params["type"].as_str().unwrap_or("log"); |
| 79 | if let Some(ref set) = filter_set { |
| 80 | if !set.contains(&msg_type.to_lowercase()) { |
| 81 | continue; |
| 82 | } |
| 83 | } |
| 84 | let args = params["args"] |
| 85 | .as_array() |
| 86 | .map(|v| v.as_slice()) |
| 87 | .unwrap_or(&[]); |
| 88 | let text = crate::cdp::join_console_args(args); |
| 89 | let timestamp = params["timestamp"].as_f64().unwrap_or(0.0); |
| 90 | |
| 91 | messages.push(json!({ |
| 92 | "type": msg_type, |
| 93 | "text": text, |
| 94 | "timestamp": timestamp, |
| 95 | })); |
| 96 | } |
| 97 | "Runtime.exceptionThrown" => { |
| 98 | if let Some(ref set) = filter_set { |
| 99 | // `set` holds lowercase types (see filter_set), so these |
| 100 | // literals must stay lowercase to match. |
| 101 | if !set.contains("exception") && !set.contains("error") { |
| 102 | continue; |