Skip to main content

glass/browser/session/
dialog.rs

1//! JavaScript dialog (alert, confirm, prompt) handling.
2//!
3//! Inspects and dismisses JavaScript dialogs via CDP `Page.javascriptDialogOpening`
4//! events. Supports accept and dismiss with optional prompt text.
5
6use super::*;
7
8impl BrowserSession {
9    /// Return the currently pending JavaScript dialog content, if any.
10    ///
11    /// Agents should read this before calling `accept_dialog` or
12    /// `dismiss_dialog` to determine the dialog type, message, and
13    /// default value. The dialog is cleared when it is handled or closed.
14    pub async fn pending_dialog(&self) -> Option<PendingDialog> {
15        self.topology.lock().await.pending_dialog.clone()
16    }
17
18    /// Accept (confirm) the currently pending JavaScript dialog.
19    ///
20    /// For `prompt` dialogs, the default prompt value is submitted.
21    /// Invalidates the observation cache after handling.
22    pub async fn accept_dialog(&self) -> BrowserResult<()> {
23        self.cdp.handle_javascript_dialog(true).await?;
24        self.invalidate_observation();
25        Ok(())
26    }
27
28    /// Dismiss (cancel) the currently pending JavaScript dialog.
29    ///
30    /// Invalidates the observation cache after handling.
31    pub async fn dismiss_dialog(&self) -> BrowserResult<()> {
32        self.cdp.handle_javascript_dialog(false).await?;
33        self.invalidate_observation();
34        Ok(())
35    }
36}