Skip to content

The headless browser for AI agents and web scraping

Merge pull request #1028 from AntonioRossi/fix/media-query-emulation-20260918

SGavrlcc18db20e5d2
diff --cc crates/obscura-browser/src/page.rs
index e6c64cea,d7a9010e..018269c9
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@@ -1105,8 -1082,8 +1106,9 @@@ impl Page
viewport: (1280.0, 720.0),
screen_size_override: None,
screen_metrics_emulated: false,
+ locale_override: None,
device_metrics_baseline: None,
+ reduced_motion: false,
device_scale_factor: 1.0,
default_background_color_override: None,
encoding: "UTF-8".to_string(),
@@@ -1843,8 -1817,8 +1852,9 @@@
rt.set_dom(dom);
}
+ rt.set_navigation_timing(self.navigation_timing.clone());
rt.run_page_init();
+ rt.set_reduced_motion(self.reduced_motion);
let _ = rt.execute_script(
"<device-metrics>",
&format!("globalThis.devicePixelRatio={};", self.device_scale_factor),
diff --cc crates/obscura-js/js/bootstrap.js
index b651b5f2,f1d5a368..cab28641
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@@ -9029,19 -8537,33 +9032,33 @@@ function _evaluateMediaQueryList(query
globalThis.matchMedia = _markNative(function matchMedia(q) {
const media = q == null ? '' : String(q);
- return {
- get matches() { return _evaluateMediaQueryList(media); },
- media,
- onchange: null,
- addListener(){},
- removeListener(){},
- addEventListener(){},
- removeEventListener(){},
- dispatchEvent(){return true;}
- };
+ return new MediaQueryList(_mediaQueryToken, media);
});
+ const _mediaQueryToken = {};
+ const _mediaQueries = new Set();
+ // A document keeps query lists with change listeners alive even when author
+ // code does not retain the object returned from matchMedia().
+ const _activeMediaQueries = new Set();
+ const _mediaQueryState = new WeakMap();
+ globalThis.__obscura_recompute_media_queries = () => {
+ // Media/viewport changes also invalidate live computed-style snapshots.
+ // Keep the epoch in this private scope, not in host-injected page script.
+ _domMutationEpoch++;
+ for (const ref of _mediaQueries) {
+ const query = ref.deref();
+ if (!query) { _mediaQueries.delete(ref); continue; }
+ const state = _mediaQueryState.get(query);
+ const matches = query.matches;
+ if (matches === state.matches) continue;
+ state.matches = matches;
+ // Capture the value at this rendering change, not at eventual delivery.
+ setTimeout(() => query.dispatchEvent(new MediaQueryListEvent('change', {
+ matches, media: query.media,
+ })), 0);
+ }
+ };
// getComputedStyle() returns a fresh declaration object, but those objects all
-// observe the same computed style for an element until the document mutates.
+// observe the same computed style until the document or viewport changes.
// Share the immutable native snapshot behind them. Frameworks routinely call
// getComputedStyle() repeatedly on the same few roots; rebuilding and parsing
// several hundred properties for every wrapper dominated real-page startup.