Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(cdp): pass preload commands directly between phases

ldm0f2cd0fd488b6
diff --git a/moli-protocol/src/domains/page.rs b/moli-protocol/src/domains/page.rs
index 621444397..7ef9cc938 100644
--- a/moli-protocol/src/domains/page.rs
+++ b/moli-protocol/src/domains/page.rs
@@ -1,13 +1,14 @@
use crate::devtools_runtime::{
- DevToolsCaptureScreenshotClip, DevToolsCaptureScreenshotCommand,
- DevToolsCaptureScreenshotResult, DevToolsCommand, DevToolsCommandContext,
- DevToolsCommandResult, DevToolsError, DevToolsErrorKind, DevToolsGetFrameTreeCommand,
- DevToolsGetFrameTreeResult, DevToolsGetFrameTreesCommand, DevToolsGetFrameTreesResult,
- DevToolsGetJavaScriptDialogCommand, DevToolsGetLayoutMetricsCommand,
- DevToolsHandleJavaScriptDialogCommand, DevToolsJavaScriptDialogResult,
- DevToolsLayoutMetricsResult, DevToolsPrintToPdfCommand, DevToolsPrintToPdfTransferMode,
- DevToolsProtocol, DevToolsScreenshotClip, DevToolsSetJavaScriptDialogPromptTextCommand,
- DevToolsTargetInfo, DevToolsTargetKind, UserPromptClosedEvent,
+ DevToolsAddPreloadScriptCommand, DevToolsCaptureScreenshotClip,
+ DevToolsCaptureScreenshotCommand, DevToolsCaptureScreenshotResult, DevToolsCommand,
+ DevToolsCommandContext, DevToolsCommandResult, DevToolsError, DevToolsErrorKind,
+ DevToolsGetFrameTreeCommand, DevToolsGetFrameTreeResult, DevToolsGetFrameTreesCommand,
+ DevToolsGetFrameTreesResult, DevToolsGetJavaScriptDialogCommand,
+ DevToolsGetLayoutMetricsCommand, DevToolsHandleJavaScriptDialogCommand,
+ DevToolsJavaScriptDialogResult, DevToolsLayoutMetricsResult, DevToolsPrintToPdfCommand,
+ DevToolsPrintToPdfTransferMode, DevToolsProtocol, DevToolsScreenshotClip,
+ DevToolsSetJavaScriptDialogPromptTextCommand, DevToolsTargetInfo, DevToolsTargetKind,
+ UserPromptClosedEvent,
};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};
use chromiumoxide_cdp::cdp::browser_protocol::page::{
@@ -212,7 +213,7 @@ enum PendingPageCommandKind {
RemoveDocumentStartScript {
pending: PendingPageCommand,
},
- AddScriptToEvaluateOnNewDocument(preload::PendingAddScriptToEvaluateOnNewDocumentCommand),
+ AddScriptToEvaluateOnNewDocument(DevToolsAddPreloadScriptCommand),
GetFrameTree {
output_kind: FrameTreeCommandOutputKind,
target_id: String,
@@ -280,7 +281,7 @@ enum CompletedPageCommandKind {
RemoveDocumentStartScript {
completed: Box<Result<CompletedPageCommand, String>>,
},
- AddScriptToEvaluateOnNewDocument(preload::CompletedAddScriptToEvaluateOnNewDocumentCommand),
+ AddScriptToEvaluateOnNewDocument(DevToolsAddPreloadScriptCommand),
GetFrameTree {
output_kind: FrameTreeCommandOutputKind,
target_id: String,
@@ -399,8 +400,8 @@ impl PendingPageCommandDispatch {
completed: Box::new(pending.wait().await.map_err(|error| error.to_string())),
}
}
- PendingPageCommandKind::AddScriptToEvaluateOnNewDocument(pending) => {
- CompletedPageCommandKind::AddScriptToEvaluateOnNewDocument(pending.wait().await)
+ PendingPageCommandKind::AddScriptToEvaluateOnNewDocument(command) => {
+ CompletedPageCommandKind::AddScriptToEvaluateOnNewDocument(command)
}
PendingPageCommandKind::GetFrameTree {
output_kind,
@@ -2018,12 +2019,11 @@ pub(crate) async fn complete_pending_page_command(
*completed,
);
}
- CompletedPageCommandKind::AddScriptToEvaluateOnNewDocument(completed) => {
+ CompletedPageCommandKind::AddScriptToEvaluateOnNewDocument(command) => {
return preload::complete_pending_add_script_to_evaluate_on_new_document_command(
conn,
- command_id,
&owner_scope,
- completed,
+ command,
command_context,
)
.await;
diff --git a/moli-protocol/src/domains/page/preload.rs b/moli-protocol/src/domains/page/preload.rs
index 12ccf3387..33d4ef1de 100644
--- a/moli-protocol/src/domains/page/preload.rs
+++ b/moli-protocol/src/domains/page/preload.rs
@@ -47,14 +47,6 @@ pub(super) struct CompletedCreateIsolatedWorldCommand {
completed: CompletedCreateIsolatedWorldPhase,
}
-pub(super) struct PendingAddScriptToEvaluateOnNewDocumentCommand {
- command: DevToolsAddPreloadScriptCommand,
-}
-
-pub(super) struct CompletedAddScriptToEvaluateOnNewDocumentCommand {
- command: DevToolsAddPreloadScriptCommand,
-}
-
struct RecordedDocumentStartScript {
identifier: String,
script: DocumentStartScript,
@@ -118,14 +110,6 @@ impl PendingCreateIsolatedWorldCommand {
}
}
-impl PendingAddScriptToEvaluateOnNewDocumentCommand {
- pub(super) async fn wait(self) -> CompletedAddScriptToEvaluateOnNewDocumentCommand {
- CompletedAddScriptToEvaluateOnNewDocumentCommand {
- command: self.command,
- }
- }
-}
-
async fn append_loaded_page_document_start_script_for_owner_async(
conn: &mut CdpConnection,
owner: &CommandOwnerScope,
@@ -1036,7 +1020,7 @@ fn start_devtools_add_preload_script_command(
command_id,
owner_scope: crate::conn::CommandOwnerScope::capture(conn, command_session_id),
kind: Box::new(PendingPageCommandKind::AddScriptToEvaluateOnNewDocument(
- PendingAddScriptToEvaluateOnNewDocumentCommand { command },
+ command,
)),
});
}
@@ -1699,16 +1683,15 @@ async fn complete_create_isolated_world_task(
pub(super) async fn complete_pending_add_script_to_evaluate_on_new_document_command(
conn: &mut CdpConnection,
- _command_id: Option<u64>,
owner: &CommandOwnerScope,
- completed: CompletedAddScriptToEvaluateOnNewDocumentCommand,
+ command: DevToolsAddPreloadScriptCommand,
command_context: &mut CommandDispatchContext,
) -> PageCommandTaskStep {
let mut plan = CommandOutputPlan::default();
match add_script_to_evaluate_on_new_document_direct_async(
conn,
owner,
- completed.command,
+ command,
&mut plan,
command_context,
)