fix(js): execute dynamic data URL scripts

Decode JavaScript data URLs inside the serialized classic-script queue so each body runs before its load event advances a nested chain.

Fixes #520.
This commit is contained in:
alectimison-maker committed 2026-07-31 04:44:29 +08:00
1 parent 3a68457c0b
commit 0c4740a86a
2 files changed
+120 -5

No files matched your search

@@ -117,3 +117,82 @@ async fn dynamic_external_scripts_execute_and_fire_load() {
"dynamic scripts must execute and fire load before navigation settles"
);
}
#[tokio::test(flavor = "current_thread")]
async fn dynamic_data_scripts_execute_before_chained_load_handlers() {
let mut ctx = CdpContext::new();
let page_id = ctx.create_page();
let session_id = "session-1";
ctx.sessions.insert(session_id.to_string(), page_id);
cdp(
&mut ctx,
1,
"Page.navigate",
json!({
"url": "data:text/html,<html><head></head><body></body></html>",
"waitUntil": "load"
}),
session_id,
)
.await;
cdp(
&mut ctx,
2,
"Runtime.evaluate",
json!({
"expression": r#"(function () {
var state = {
aExec: false,
aLoad: false,
bExec: false,
bLoad: false,
cExec: false,
cLoad: false
};
window.__dataScriptState = state;
var a = document.createElement('script');
a.src = 'data:text/javascript,window.__dataScriptState.aExec=true';
a.onload = function () {
state.aLoad = true;
var b = document.createElement('script');
b.src = 'data:application/javascript,' + encodeURIComponent('window.__dataScriptState.bExec=true');
b.onload = function () {
state.bLoad = true;
var c = document.createElement('script');
c.src = 'data:text/javascript;base64,' + btoa('window.__dataScriptState.cExec=true');
c.onload = function () { state.cLoad = true; };
document.head.appendChild(c);
};
document.head.appendChild(b);
};
document.head.appendChild(a);
return 'kicked';
})()"#,
"returnByValue": true,
}),
session_id,
)
.await;
ctx.pages[0].settle(500).await;
let result = cdp(
&mut ctx,
3,
"Runtime.evaluate",
json!({
"expression": "JSON.stringify(window.__dataScriptState)",
"returnByValue": true,
}),
session_id,
)
.await;
assert_eq!(
result["result"]["value"],
r#"{"aExec":true,"aLoad":true,"bExec":true,"bLoad":true,"cExec":true,"cLoad":true}"#,
"data URL script bodies must execute before each chained load handler"
);
}