The headless browser for AI agents and web scraping
fix(js): make Web IDL operations enumerable on interface prototypes
Yubo-Caof645df222bb6
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 2735832d..1ccfbb3f 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -15922,6 +15922,59 @@ if (typeof Response !== 'undefined' && Response.prototype && !Response.prototype_iframeRealmGlobalNameSet = new Set(_iframeRealmGlobalNames);})();+// WebIDL creates interface operations on the interface prototype object with+// { writable: true, enumerable: true, configurable: true }+// (https://webidl.spec.whatwg.org/#dfn-create-operation-function), so in a real+// browser `Object.keys(MutationObserver.prototype)` is+// ['observe', 'disconnect', 'takeRecords']. ES class methods are+// enumerable: false, so every interface written as a `class` here disagrees with+// the platform on that one descriptor bit.+//+// zone.js — which Angular installs on every page — discovers methods by walking+// `for (prop in instance)` in patchClass(), applied to MutationObserver,+// WebKitMutationObserver, IntersectionObserver and FileReader. With+// enumerable: false it finds no methods at all and builds its proxy prototype+// out of whatever instance fields happen to be enumerable, so the patched class+// ends up with `_callback`/`_targets`/`_records` and no `observe`. Angular's+// router then dies on `TypeError: n.observe is not a function` and the page+// never renders. #245 was the same disagreement on the+// getOwnPropertyDescriptor path; this is the for-in path.+//+// Only spec operations are exposed. Internals stay hidden behind the `_` prefix,+// which keeps Object.keys() output identical to Chrome's.+(function _markWebIdlOperationsEnumerable() {+ var INTERFACES = [+ 'MutationObserver',+ 'IntersectionObserver',+ 'ResizeObserver',+ 'PerformanceObserver',+ 'FileReader',+ ];+ for (var i = 0; i < INTERFACES.length; i++) {+ var ctor;+ try { ctor = globalThis[INTERFACES[i]]; } catch (e) { continue; }+ if (typeof ctor !== 'function' || !ctor.prototype) { continue; }+ var proto = ctor.prototype;+ var keys = Object.getOwnPropertyNames(proto);+ for (var k = 0; k < keys.length; k++) {+ var key = keys[k];+ if (key === 'constructor' || key.charAt(0) === '_') { continue; }+ var d;+ try { d = Object.getOwnPropertyDescriptor(proto, key); } catch (e) { continue; }+ if (!d || d.enumerable || !d.configurable) { continue; }+ if (typeof d.value !== 'function') { continue; }+ try {+ Object.defineProperty(proto, key, {+ value: d.value,+ writable: d.writable,+ enumerable: true,+ configurable: true,+ });+ } catch (e) {}+ }+ }+})();+(function _markBuiltinsNative() {var seen = new Set();function walk(ctor) {diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex d9d7f2b9..e203cfc7 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -20009,6 +20009,97 @@ mod tests {);}+ // WebIDL puts interface operations on the interface prototype with+ // enumerable: true, so in a browser Object.keys(MutationObserver.prototype)+ // is ['observe', 'disconnect', 'takeRecords']. ES class methods are+ // enumerable: false. Internals must stay hidden so the key set still matches+ // Chrome exactly. See _markWebIdlOperationsEnumerable in bootstrap.js.+ #[test]+ fn webidl_operations_are_enumerable_on_interface_prototypes() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(+ r#"+ var scriptTestSetup = true;+ const descriptor =+ Object.getOwnPropertyDescriptor(MutationObserver.prototype, 'observe');+ return JSON.stringify({+ mutationObserver: Object.keys(MutationObserver.prototype).sort().join(','),+ intersectionObserver: Object.keys(IntersectionObserver.prototype).includes('observe'),+ resizeObserver: Object.keys(ResizeObserver.prototype).includes('observe'),+ performanceObserver: Object.keys(PerformanceObserver.prototype).includes('observe'),+ internalsHidden: !Object.keys(MutationObserver.prototype).includes('_notify'),+ writable: descriptor.writable,+ configurable: descriptor.configurable,+ });+ "#,+ )+ .unwrap();+ let seen: serde_json::Value =+ serde_json::from_str(result.as_str().unwrap()).expect("descriptor json");+ assert_eq!(seen["mutationObserver"], "disconnect,observe,takeRecords");+ assert_eq!(seen["intersectionObserver"], true);+ assert_eq!(seen["resizeObserver"], true);+ assert_eq!(seen["performanceObserver"], true);+ assert_eq!(seen["internalsHidden"], true);+ // Still a method descriptor, not a data property masquerading as one.+ assert_eq!(seen["writable"], true);+ assert_eq!(seen["configurable"], true);+ }++ // Mirrors zone.js patchClass(), which Angular installs for MutationObserver:+ // it builds a proxy prototype by walking `for (prop in instance)` and+ // forwarding every function-valued property to the original instance. With+ // enumerable: false methods that loop found nothing, so the patched class had+ // no observe() and Angular's router died on "n.observe is not a function"+ // before rendering anything. #245 was the same disagreement on the+ // getOwnPropertyDescriptor path.+ #[test]+ fn zone_js_style_class_patch_finds_observer_operations() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(+ r#"+ var scriptTestSetup = true;+ const Original = globalThis.MutationObserver;+ const ORIGINAL_INSTANCE = '__zone_symbol__originalInstance';+ function Patched(...args) { this[ORIGINAL_INSTANCE] = new Original(...args); }+ const forwarded = [];+ const probe = new Original(function () {});+ for (const prop in probe) {+ if (typeof probe[prop] !== 'function') continue;+ forwarded.push(prop);+ Patched.prototype[prop] = function () {+ return this[ORIGINAL_INSTANCE][prop].apply(this[ORIGINAL_INSTANCE], arguments);+ };+ }++ // Drive the patched class the way Angular drives it.+ const observer = new Patched(() => {});+ observer.observe(document.body, { childList: true });+ document.body.appendChild(document.createElement('span'));+ const taken = observer.takeRecords().length;+ observer.disconnect();+ return JSON.stringify({ forwarded: forwarded.sort().join(','), taken });+ "#,+ )+ .unwrap();+ let patched: serde_json::Value =+ serde_json::from_str(result.as_str().unwrap()).expect("patch json");+ for operation in ["observe", "disconnect", "takeRecords"] {+ assert!(+ patched["forwarded"]+ .as_str()+ .expect("forwarded list")+ .contains(operation),+ "zone.js-style for-in patch must discover {operation}, found: {}",+ patched["forwarded"]+ );+ }+ // And the forwarded observe() actually observes.+ assert_eq!(patched["taken"], 1);+ }+// Writing goes through the same insertion steps as any other insertion.#[test]fn document_write_reports_to_mutation_observers() {