BlueGit

ict rusty-teams

fix(calls): retry the token script once while the tab still loads

- removes the localStorage Access is denied failure at engine start

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-05 · 3 weeks ago

Hash
2f89f39567ed74f3d117e2c180b4a9d91a73b4d8
Parent
32b96f54
1 file changed +13 −4
crates/session/src/page_script.rs +13 −4
@@ -1,3 +1,5 @@
1 use std::time::Duration;
2
1 3 use serde_json::{Value, json};
2 4
3 5 use crate::app::App;
@@ -7,6 +9,8 @@ use crate::request::Outcome;
7 9 use crate::scope::Scope;
8 10 use crate::session::{FETCH_SCRIPT, NOT_GRANTED_PREFIX, Session};
9 11
12 const LOADING_DOCUMENT_DENIAL: &str = "Access is denied for this document";
13 const LOADING_DOCUMENT_WAIT: Duration = Duration::from_secs(1);
10 14 const TOKEN_MARKER: &str = "if (!token) return {noToken: true, refreshError, page};";
11 15 const ACQUIRE_CALL: &str = "const {token, refreshError} = await acquire(resource, scope);";
12 16
@@ -56,10 +60,15 @@ impl Session {
56 60 head = &FETCH_SCRIPT[..marker_at]
57 61 );
58 62 let expression = format!("({script})({payload})");
59 let outcome = self
60 .evaluate_in(app, &expression)
61 .await?
62 .ok_or(Error::NoAppTab)?;
63 // Right after launch the app tab can still hold a document without storage access; it settles within a second.
64 let outcome = match self.evaluate_in(app, &expression).await {
65 Err(Error::Cdp(text)) if text.contains(LOADING_DOCUMENT_DENIAL) => {
66 tokio::time::sleep(LOADING_DOCUMENT_WAIT).await;
67 self.evaluate_in(app, &expression).await
68 }
69 outcome => outcome,
70 }?
71 .ok_or(Error::NoAppTab)?;
63 72 if outcome.get("noToken").and_then(Value::as_bool) == Some(true) {
64 73 let outcome: Outcome = serde_json::from_value(outcome)
65 74 .map_err(|error| Error::Cdp(format!("unexpected page answer: {error}")))?;
@@ -1,3 +1,5 @@
1use std::time::Duration;
2
1use serde_json::{Value, json};
3use serde_json::{Value, json};
2
4
3use crate::app::App;
5use crate::app::App;
@@ -7,6 +9,8 @@ use crate::request::Outcome;
7use crate::scope::Scope;
9use crate::scope::Scope;
8use crate::session::{FETCH_SCRIPT, NOT_GRANTED_PREFIX, Session};
10use crate::session::{FETCH_SCRIPT, NOT_GRANTED_PREFIX, Session};
9
11
12const LOADING_DOCUMENT_DENIAL: &str = "Access is denied for this document";
13const LOADING_DOCUMENT_WAIT: Duration = Duration::from_secs(1);
10const TOKEN_MARKER: &str = "if (!token) return {noToken: true, refreshError, page};";
14const TOKEN_MARKER: &str = "if (!token) return {noToken: true, refreshError, page};";
11const ACQUIRE_CALL: &str = "const {token, refreshError} = await acquire(resource, scope);";
15const ACQUIRE_CALL: &str = "const {token, refreshError} = await acquire(resource, scope);";
12
16
@@ -56,10 +60,15 @@ impl Session {
56 head = &FETCH_SCRIPT[..marker_at]
60 head = &FETCH_SCRIPT[..marker_at]
57 );
61 );
58 let expression = format!("({script})({payload})");
62 let expression = format!("({script})({payload})");
59 let outcome = self
63 // Right after launch the app tab can still hold a document without storage access; it settles within a second.
60 .evaluate_in(app, &expression)
64 let outcome = match self.evaluate_in(app, &expression).await {
61 .await?
65 Err(Error::Cdp(text)) if text.contains(LOADING_DOCUMENT_DENIAL) => {
62 .ok_or(Error::NoAppTab)?;
66 tokio::time::sleep(LOADING_DOCUMENT_WAIT).await;
67 self.evaluate_in(app, &expression).await
68 }
69 outcome => outcome,
70 }?
71 .ok_or(Error::NoAppTab)?;
63 if outcome.get("noToken").and_then(Value::as_bool) == Some(true) {
72 if outcome.get("noToken").and_then(Value::as_bool) == Some(true) {
64 let outcome: Outcome = serde_json::from_value(outcome)
73 let outcome: Outcome = serde_json::from_value(outcome)
65 .map_err(|error| Error::Cdp(format!("unexpected page answer: {error}")))?;
74 .map_err(|error| Error::Cdp(format!("unexpected page answer: {error}")))?;