Skip to content

The headless browser for AI agents and web scraping

fix(js): navigate to fragments in the same document

Floyd Maan8a7aa8c3eb76
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 9d45822f..9272519c 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -4583,7 +4583,7 @@ class Element extends Node {
const link = this.tagName === 'A' ? this : (this.closest ? this.closest('a[href]') : null);
if (link) {
const href = link.getAttribute('href');
- if (href && !href.startsWith('#') && !href.startsWith('javascript:')) {
+ if (href && !href.startsWith('javascript:')) {
location.assign(href);
return;
}
@@ -6281,7 +6281,7 @@ class Document extends Node {
get location() { return this._detached ? null : globalThis.location; }
set location(url) {
if (this._detached) return;
- __obscuraCore.ops.op_navigate(_resolveUrl(String(url)), 'GET', '');
+ __locationNavigate(String(url), false);
}
get defaultView() {
if (this._detached) return null;
@@ -7661,26 +7661,74 @@ globalThis.__virtualUrl = null;
function __currentUrl() {
return globalThis.__virtualUrl || _domParse("document_url") || "about:blank";
}
+// HTML "navigate": a target that has a fragment and otherwise equals the
+// current URL is a same-document fragment navigation. It must not load a new
+// document; the history IIFE below adds (or replaces) a session-history entry
+// and queues hashchange. Hash routers (`location.hash = '#/route'`,
+// `<a href="#tab">`) depend on this.
+function __stripFragment(url) {
+ var i = url.indexOf('#');
+ return i < 0 ? url : url.slice(0, i);
+}
+function __locationNavigate(url, replace) {
+ var r = _resolveUrl(url);
+ var target = r;
+ try { target = new URL(r).href; } catch (e) {}
+ var current = __currentUrl();
+ if (target.indexOf('#') >= 0 && __stripFragment(target) === __stripFragment(current) &&
+ typeof globalThis.__obscuraNavigateToFragment === 'function') {
+ globalThis.__obscuraNavigateToFragment(target, replace || target === current);
+ return;
+ }
+ globalThis.__virtualUrl = r;
+ __obscuraCore.ops.op_navigate(r, 'GET', '');
+}
+// Location setters (HTML spec): change one component of a copy of the current
+// URL, then navigate to it. Only the fragment setter can stay in-document.
+function __locationSetPart(part, value) {
+ var u;
+ try { u = new URL(__currentUrl()); } catch (e) { return; }
+ if (part === 'hash') {
+ var current = u.href;
+ var input = String(value);
+ if (input.startsWith('#')) input = input.slice(1);
+ u.hash = input;
+ var next = input === '' ? __stripFragment(current) + '#' : u.href;
+ var oldFragment = current.indexOf('#') >= 0 ? current.slice(current.indexOf('#')) : null;
+ var newFragment = next.slice(next.indexOf('#'));
+ if (oldFragment === newFragment) return;
+ __locationNavigate(next, false);
+ return;
+ }
+ u[part] = String(value);
+ __locationNavigate(u.href, false);
+}
globalThis.location = {
get href() { return __currentUrl(); },
- set href(url) { var r = _resolveUrl(url); globalThis.__virtualUrl = r; __obscuraCore.ops.op_navigate(r, 'GET', ''); },
+ set href(url) { __locationNavigate(url, false); },
get origin() { try { return new URL(this.href).origin; } catch { return ""; } },
get protocol() { try { return new URL(this.href).protocol; } catch { return ""; } },
get host() { try { return new URL(this.href).host; } catch { return ""; } },
+ set host(v) { __locationSetPart('host', v); },
get hostname() { try { return new URL(this.href).hostname; } catch { return ""; } },
+ set hostname(v) { __locationSetPart('hostname', v); },
get pathname() { try { return new URL(this.href).pathname; } catch { return "/"; } },
+ set pathname(v) { __locationSetPart('pathname', v); },
get search() { try { return new URL(this.href).search; } catch { return ""; } },
+ set search(v) { __locationSetPart('search', v); },
get hash() { try { return new URL(this.href).hash; } catch { return ""; } },
+ set hash(v) { __locationSetPart('hash', v); },
get port() { try { return new URL(this.href).port; } catch { return ""; } },
+ set port(v) { __locationSetPart('port', v); },
toString() { return this.href; },
- assign(url) { var r = _resolveUrl(url); globalThis.__virtualUrl = r; __obscuraCore.ops.op_navigate(r, 'GET', ''); },
+ assign(url) { __locationNavigate(url, false); },
reload() { var r = _resolveUrl(this.href); globalThis.__virtualUrl = r; __obscuraCore.ops.op_navigate(r, 'GET', ''); },
- replace(url) { var r = _resolveUrl(url); globalThis.__virtualUrl = r; __obscuraCore.ops.op_navigate(r, 'GET', ''); },
+ replace(url) { __locationNavigate(url, true); },
};
const _locationObj = globalThis.location;
Object.defineProperty(globalThis, 'location', {
get() { return _locationObj; },
- set(url) { var r = _resolveUrl(String(url)); globalThis.__virtualUrl = r; __obscuraCore.ops.op_navigate(r, 'GET', ''); },
+ set(url) { __locationNavigate(String(url), false); },
configurable: false,
enumerable: true,
});
@@ -13140,6 +13188,31 @@ globalThis.atob = globalThis.atob || ((s) => {
const entry = stack[idx];
globalThis.__virtualUrl = entry.url ?? null;
};
+ // Same-document fragment navigation, reached from the Location setters and
+ // `<a href="#x">` activation. Per the HTML spec it adds a history entry
+ // (or replaces the current one), keeps the document, and queues a
+ // hashchange task when the fragment changed. No popstate fires here.
+ Object.defineProperty(globalThis, "__obscuraNavigateToFragment", {
+ value: (url, replace) => {
+ const prevUrl = __currentUrl();
+ if (replace) {
+ stack[idx] = {state: null, url};
+ } else {
+ stack.length = idx + 1;
+ stack.push({state: null, url});
+ idx = stack.length - 1;
+ forwardCleared = true;
+ }
+ applyVirtual();
+ if (prevUrl === url) return;
+ setTimeout(() => {
+ const ev = new HashChangeEvent('hashchange');
+ ev.oldURL = prevUrl; ev.newURL = url;
+ try { globalThis.dispatchEvent(ev); } catch {}
+ }, 0);
+ },
+ configurable: true,
+ });
const fireHashChangeIfNeeded = (prevUrl) => {
try {
const next = __currentUrl();
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 114dc7c2..76e7fb78 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -6935,6 +6935,72 @@ mod tests {
assert_eq!(path, serde_json::json!("/dashboard"));
}
+ #[tokio::test(flavor = "current_thread")]
+ async fn location_hash_navigates_to_a_fragment_in_the_same_document() {
+ let mut rt = setup_runtime("<html><body><a id=tab href='#tab'>tab</a></body></html>");
+ rt.execute_script(
+ "fragment-navigation",
+ r#"
+ globalThis.__events = [];
+ addEventListener('hashchange', (e) => __events.push(e.oldURL + ' -> ' + e.newURL));
+ globalThis.__marker = 'same-document';
+ location.hash = 'one';
+ globalThis.__syncHash = location.hash;
+ location.hash = '#one';
+ location.href = '#two';
+ document.getElementById('tab').click();
+ "#,
+ )
+ .unwrap();
+ assert_eq!(rt.take_pending_navigation(), None, "a fragment change must not load a new document");
+ rt.run_event_loop_bounded(100).await.unwrap();
+ let result = rt
+ .evaluate("[__syncHash, location.href, history.length, __marker, __events]")
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!([
+ "#one",
+ "http://example.com/test#tab",
+ 4,
+ "same-document",
+ [
+ "http://example.com/test -> http://example.com/test#one",
+ "http://example.com/test#one -> http://example.com/test#two",
+ "http://example.com/test#two -> http://example.com/test#tab",
+ ],
+ ])
+ );
+ rt.evaluate("history.back()").unwrap();
+ assert_eq!(rt.evaluate("location.hash").unwrap(), serde_json::json!("#two"));
+ }
+
+ #[test]
+ fn location_replace_with_a_fragment_replaces_the_history_entry() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt
+ .evaluate("(function(){ location.replace('#r'); return [location.hash, history.length]; })()")
+ .unwrap();
+ assert_eq!(result, serde_json::json!(["#r", 1]));
+ assert_eq!(rt.take_pending_navigation(), None);
+ }
+
+ #[test]
+ fn location_component_setters_load_the_new_url() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ rt.evaluate("location.search = 'q=1'").unwrap();
+ assert_eq!(
+ rt.take_pending_navigation(),
+ Some(("http://example.com/test?q=1".into(), "GET".into(), String::new())),
+ );
+ let mut rt = setup_runtime("<html><body></body></html>");
+ rt.evaluate("location.pathname = '/other'").unwrap();
+ assert_eq!(
+ rt.take_pending_navigation().map(|(url, _, _)| url),
+ Some("http://example.com/other".into()),
+ );
+ }
+
#[test]
fn push_state_without_url_preserves_current_location() {
let mut rt = setup_runtime("<html><body></body></html>");