Skip to content

The headless browser for AI agents and web scraping

fix(js): preserve fragment history state

SGavrlfb23f18bb6a1
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 9272519c..1448b9ff 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -7677,7 +7677,7 @@ function __locationNavigate(url, replace) {
var current = __currentUrl();
if (target.indexOf('#') >= 0 && __stripFragment(target) === __stripFragment(current) &&
typeof globalThis.__obscuraNavigateToFragment === 'function') {
- globalThis.__obscuraNavigateToFragment(target, replace || target === current);
+ globalThis.__obscuraNavigateToFragment(target, replace);
return;
}
globalThis.__virtualUrl = r;
@@ -7689,15 +7689,11 @@ function __locationSetPart(part, value) {
var u;
try { u = new URL(__currentUrl()); } catch (e) { return; }
if (part === 'hash') {
- var current = u.href;
+ var previousHash = u.hash;
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);
+ u.hash = input === '' ? '#' : input;
+ if (u.hash === previousHash) return;
+ __locationNavigate(u.href, false);
return;
}
u[part] = String(value);
@@ -13195,6 +13191,7 @@ globalThis.atob = globalThis.atob || ((s) => {
Object.defineProperty(globalThis, "__obscuraNavigateToFragment", {
value: (url, replace) => {
const prevUrl = __currentUrl();
+ if (prevUrl === url) return;
if (replace) {
stack[idx] = {state: null, url};
} else {
@@ -13204,7 +13201,6 @@ globalThis.atob = globalThis.atob || ((s) => {
forwardCleared = true;
}
applyVirtual();
- if (prevUrl === url) return;
setTimeout(() => {
const ev = new HashChangeEvent('hashchange');
ev.oldURL = prevUrl; ev.newURL = url;
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 76e7fb78..b89068d9 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -7001,6 +7001,46 @@ mod tests {
);
}
+ #[tokio::test(flavor = "current_thread")]
+ async fn location_hash_preserves_leading_hashes_and_ignores_an_unchanged_empty_fragment() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ rt.execute_script("hash-setter", r#"
+ globalThis.__hashEvents = [];
+ addEventListener('hashchange', e => __hashEvents.push([e.oldURL, e.newURL]));
+ location.hash = '';
+ location.hash = '#';
+ globalThis.__emptyHashResult = [location.href, history.length];
+ location.hash = '##panel';
+ location.hash = '##panel';
+ "#).unwrap();
+ assert_eq!(rt.take_pending_navigation(), None);
+ rt.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(rt.evaluate("[__emptyHashResult, location.href, location.hash, history.length, __hashEvents]").unwrap(),
+ serde_json::json!([
+ ["http://example.com/test", 1],
+ "http://example.com/test##panel", "##panel", 2,
+ [["http://example.com/test", "http://example.com/test##panel"]],
+ ]));
+ }
+
+ #[tokio::test(flavor = "current_thread")]
+ async fn same_fragment_navigation_preserves_history_state() {
+ let mut rt = setup_runtime("<html><body><a id=tab href='#panel'>tab</a></body></html>");
+ rt.execute_script("same-fragment", r#"
+ history.replaceState({saved: 1}, '', '#panel');
+ globalThis.__sameHashEvents = 0;
+ addEventListener('hashchange', () => __sameHashEvents++);
+ location.assign('#panel');
+ location.href = '#panel';
+ location.replace('#panel');
+ document.getElementById('tab').click();
+ "#).unwrap();
+ rt.run_event_loop_bounded(100).await.unwrap();
+ assert_eq!(rt.take_pending_navigation(), None);
+ assert_eq!(rt.evaluate("[history.state, history.length, location.hash, __sameHashEvents]").unwrap(),
+ serde_json::json!([{ "saved": 1 }, 1, "#panel", 0]));
+ }
+
#[test]
fn push_state_without_url_preserves_current_location() {
let mut rt = setup_runtime("<html><body></body></html>");