MCPcopy Create free account
hub / github.com/aeroxy/chrome-devtools-cli / fill

Function fill

src/commands/input.rs:236–360  ·  view source on GitHub ↗

Fill an input field identified by CSS selector with the given value.

(
    client: &mut CdpClient,
    session_id: &str,
    selector: &str,
    value: &str,
)

Source from the content-addressed store, hash-verified

234
235/// Fill an input field identified by CSS selector with the given value.
236pub async fn fill(
237 client: &mut CdpClient,
238 session_id: &str,
239 selector: &str,
240 value: &str,
241) -> Result<CommandResult> {
242 let initial_url = client.current_url(session_id).await?;
243 let escaped_sel = serde_json::to_string(selector)?;
244 // Escape value for safely injecting into JS
245 let escaped_val = serde_json::to_string(value)?;
246
247 let expr = format!(
248 r#"(() => {{
249 const el = document.querySelector({escaped_sel});
250 if (!el) return 'not_found';
251
252 const tagName = el.tagName.toLowerCase();
253 const type = el.type ? el.type.toLowerCase() : '';
254
255 if (tagName === 'select') {{
256 let optionFound = false;
257 for (const option of el.options) {{
258 if (option.value === {escaped_val} || option.text === {escaped_val}) {{
259 el.value = option.value;
260 optionFound = true;
261 break;
262 }}
263 }}
264 if (!optionFound) {{
265 return 'option_not_found';
266 }}
267 el.dispatchEvent(new Event('input', {{bubbles: true}}));
268 el.dispatchEvent(new Event('change', {{bubbles: true}}));
269 return 'select_ok';
270 }}
271
272if (tagName === 'input' && (type === 'checkbox' || type === 'radio')) {{
273 const isTrue = {escaped_val}.toLowerCase() === 'true';
274 if (el.checked !== isTrue) {{
275 el.checked = isTrue;
276 el.dispatchEvent(new Event('input', {{bubbles: true}}));
277 el.dispatchEvent(new Event('change', {{bubbles: true}}));
278 }}
279 return 'checkbox_ok';
280 }}
281
282 if (tagName === 'textarea') {{
283 el.value = {escaped_val};
284 el.dispatchEvent(new Event('input', {{bubbles: true}}));
285 el.dispatchEvent(new Event('change', {{bubbles: true}}));
286 return 'textarea_ok';
287 }}
288
289 if (type === 'file') {{
290 return 'file_input';
291 }}
292
293 // For contenteditable elements or other text-like elements

Callers 2

run_directFunction · 0.85
inner_executeFunction · 0.85

Calls 3

current_urlMethod · 0.80
send_to_targetMethod · 0.80

Tested by

no test coverage detected