Skip to content

The headless browser for AI agents and web scraping

merge: refresh lifecycle contribution on upstream 6393af1

Antonio Rossi7dd864338e85
diff --cc crates/obscura-browser/src/page.rs
index 9385947f,78f4f4ec..4d75564f
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@@ -6865,6 -6953,77 +6953,102 @@@ mod tests
);
}
++ #[tokio::test(flavor = "current_thread")]
++ async fn document_close_waits_for_written_parser_blocking_scripts() {
++ let (base, requests) = spawn_delayed_classic_script_server(
++ std::time::Duration::from_millis(75),
++ "globalThis.__closeOrder.push('external');",
++ );
++ let mut page = import_map_test_page(
++ "document-close-parser-blocking", "http://127.0.0.1:9",
++ "<html><body></body></html>",
++ );
++ page.js.as_mut().unwrap().execute_script("replacement", &format!(r#"
++ document.open();
++ globalThis.__closeOrder = [];
++ document.addEventListener('DOMContentLoaded', () => __closeOrder.push('dom'), {{once:true}});
++ window.addEventListener('load', () => __closeOrder.push('load'), {{once:true}});
++ document.write('<script src="{base}/written.js"><\/script>');
++ document.write('<script>globalThis.__closeOrder.push("inline");<\/script>');
++ document.close();
++ "#)).unwrap();
++ page.settle(250).await;
++ assert_eq!(requests.recv_timeout(std::time::Duration::from_secs(1)).unwrap(), "/written.js");
++ assert_eq!(page.js.as_mut().unwrap().evaluate("__closeOrder").unwrap(),
++ serde_json::json!(["external", "inline", "dom", "load"]));
++ }
++
+ #[tokio::test(flavor = "current_thread")]
+ async fn document_write_external_script_blocks_later_parser_scripts() {
+ let (base, requests) = spawn_delayed_classic_script_server(
+ std::time::Duration::from_millis(75),
+ "globalThis.__writtenOrder.push('external'); globalThis.__writtenValue = 1;",
+ );
+ let html = format!(
+ r#"<html><body>
+ <script>globalThis.__writtenOrder = [];</script>
+ <script>
+ document.write('<script src="{base}/written.js"><\/script>');
+ document.write('<script>globalThis.__writtenOrder.push("written:" + String(globalThis.__writtenValue));<\/script>');
+ </script>
+ <script>globalThis.__writtenOrder.push('after:' + String(globalThis.__writtenValue));</script>
+ </body></html>"#,
+ );
+ let mut page = import_map_test_page(
+ "document-write-parser-blocking",
+ "http://127.0.0.1:9",
+ &html,
+ );
+
+ page.execute_scripts().await;
+
+ assert_eq!(
+ requests
+ .recv_timeout(std::time::Duration::from_secs(1))
+ .unwrap(),
+ "/written.js",
+ );
+ assert_eq!(
+ page.js
+ .as_mut()
+ .unwrap()
+ .evaluate("globalThis.__writtenOrder")
+ .unwrap(),
+ serde_json::json!(["external", "written:1", "after:1"]),
+ );
+ }
+
+ #[tokio::test(flavor = "current_thread")]
+ async fn document_write_does_not_start_later_async_script_before_blocker() {
+ let base = spawn_written_script_order_server();
+ let html = format!(
+ r#"<html><body><script>
+ document.write('<script src="{base}/vendor.js"><\/script>');
+ document.write('<script async src="{base}/app.js"><\/script>');
+ </script><script>
+ globalThis.__afterWriteSawVendor = globalThis.__vendorValue;
+ </script></body></html>"#,
+ );
+ let mut page = import_map_test_page(
+ "document-write-async-after-blocker",
+ "http://127.0.0.1:9",
+ &html,
+ );
+
+ page.execute_scripts().await;
+
+ assert_eq!(
+ page.js
+ .as_mut()
+ .unwrap()
+ .evaluate(
+ "[globalThis.__afterWriteSawVendor, globalThis.__appSawVendor, globalThis.__appRequestFollowedVendor]"
+ )
+ .unwrap(),
+ serde_json::json!([1, 1, true]),
+ );
+ }
+
#[tokio::test(flavor = "current_thread")]
async fn load_delaying_script_progresses_through_continuously_ready_timer_work() {
let (base, requests) = spawn_delayed_classic_script_server(
diff --cc crates/obscura-js/js/bootstrap.js
index fb1c0ed1,82576e07..c1fcdde6
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@@ -5897,25 -5986,7 +5991,29 @@@ class Document extends Node
return this;
}
close() {
- return;
+ if (!this._writeOpen) return;
+ this._writeOpen = false;
+ const generation = this._writeGeneration;
- setTimeout(() => {
++ const finishParsing = () => {
+ if (generation !== this._writeGeneration) return;
++ // document.write's parser-blocking queue may still be fetching a
++ // classic script. It must finish before DOMContentLoaded is observable.
++ if (__parserBlockingScriptPending > 0) { setTimeout(finishParsing, 1); return; }
+ globalThis.__documentReadyState__ = 'interactive';
+ this.dispatchEvent(new Event('readystatechange'));
+ this.dispatchEvent(new Event('DOMContentLoaded', { bubbles: true }));
+ _dom('document_lifecycle', 'DOMContentLoaded');
+ const complete = () => {
+ if (generation !== this._writeGeneration) return;
+ if (__dynLoadDelayingPending > 0) { setTimeout(complete, 1); return; }
+ globalThis.__documentReadyState__ = 'complete';
+ this.dispatchEvent(new Event('readystatechange'));
+ globalThis.dispatchEvent(new Event('load'));
+ _dom('document_lifecycle', 'load');
+ };
+ complete();
- }, 0);
++ };
++ setTimeout(finishParsing, 0);
}
hasFocus() { return true; }
execCommand() { return false; }