From 66f86b851cd5bceb5d72ef2642780dd78fd1961e Mon Sep 17 00:00:00 2001 From: Ravindu Liyanapathirana Date: Wed, 18 Jun 2025 23:06:53 +1000 Subject: [PATCH 1/4] Manipulate location with URL object, in goUp --- content_scripts/mode_normal.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/content_scripts/mode_normal.js b/content_scripts/mode_normal.js index eaa649c3f..b94cfaf40 100644 --- a/content_scripts/mode_normal.js +++ b/content_scripts/mode_normal.js @@ -135,16 +135,17 @@ const NormalModeCommands = { // Url manipulation. goUp(count) { - let url = globalThis.location.href; - if (url.endsWith("/")) { - url = url.substring(0, url.length - 1); + const url = new URL(globalThis.location.href); + + // Pop path segments. + if (url.pathname != "/") { + url.pathname = url.pathname.split("/").slice(0, -count).join("/"); + url.search = ""; + url.hash = ""; } - let urlsplit = url.split("/"); - // make sure we haven't hit the base domain yet - if (urlsplit.length > 3) { - urlsplit = urlsplit.slice(0, Math.max(3, urlsplit.length - count)); - globalThis.location.href = urlsplit.join("/"); + if (globalThis.location.href !== url.toString()) { + globalThis.location.href = url.toString(); } }, From 4a0b64efd639298d7c03be7890539fdfd706298c Mon Sep 17 00:00:00 2001 From: Ravindu Liyanapathirana Date: Wed, 18 Jun 2025 23:42:56 +1000 Subject: [PATCH 2/4] Add popAnchor option to goUp --- background_scripts/all_commands.js | 3 +++ content_scripts/mode_normal.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/background_scripts/all_commands.js b/background_scripts/all_commands.js index 5abab2a16..1ff98cb31 100644 --- a/background_scripts/all_commands.js +++ b/background_scripts/all_commands.js @@ -130,6 +130,9 @@ const allCommands = [ desc: "Go up the URL hierarchy", group: "navigation", advanced: true, + options: { + popAnchor: "Remove the anchor/fragment/hash from the URL, if present.", + }, }, { diff --git a/content_scripts/mode_normal.js b/content_scripts/mode_normal.js index b94cfaf40..a331b7aba 100644 --- a/content_scripts/mode_normal.js +++ b/content_scripts/mode_normal.js @@ -134,14 +134,22 @@ const NormalModeCommands = { }, // Url manipulation. - goUp(count) { + goUp(count, { registryEntry }) { + let c = count; const url = new URL(globalThis.location.href); + // Pop anchor. + if (c > 0 && registryEntry.options.popAnchor && url.hash !== "") { + url.hash = ""; + --c; + } + // Pop path segments. - if (url.pathname != "/") { - url.pathname = url.pathname.split("/").slice(0, -count).join("/"); + if (c > 0 && url.pathname != "/") { + url.pathname = url.pathname.split("/").slice(0, -c).join("/"); url.search = ""; url.hash = ""; + --c; } if (globalThis.location.href !== url.toString()) { From 9520162b8ca9369b302863a37220bd58c07e0dc4 Mon Sep 17 00:00:00 2001 From: Ravindu Liyanapathirana Date: Thu, 19 Jun 2025 00:07:15 +1000 Subject: [PATCH 3/4] Add popQuery option to goUp --- background_scripts/all_commands.js | 1 + content_scripts/mode_normal.js | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/background_scripts/all_commands.js b/background_scripts/all_commands.js index 1ff98cb31..18aee871e 100644 --- a/background_scripts/all_commands.js +++ b/background_scripts/all_commands.js @@ -132,6 +132,7 @@ const allCommands = [ advanced: true, options: { popAnchor: "Remove the anchor/fragment/hash from the URL, if present.", + popQuery: "Remove query parameters from the URL, if present.", }, }, diff --git a/content_scripts/mode_normal.js b/content_scripts/mode_normal.js index a331b7aba..f92f44b8e 100644 --- a/content_scripts/mode_normal.js +++ b/content_scripts/mode_normal.js @@ -144,6 +144,13 @@ const NormalModeCommands = { --c; } + // Pop query params. + if (c > 0 && registryEntry.options.popQuery && url.search !== "") { + url.search = ""; + url.hash = ""; + --c; + } + // Pop path segments. if (c > 0 && url.pathname != "/") { url.pathname = url.pathname.split("/").slice(0, -c).join("/"); From 441acf0bdc83a05e1c6646d3627c2fa9080b7a46 Mon Sep 17 00:00:00 2001 From: Ravindu Liyanapathirana Date: Thu, 19 Jun 2025 10:51:09 +1000 Subject: [PATCH 4/4] Fix how count is decremented when goUp pops path segments --- content_scripts/mode_normal.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/content_scripts/mode_normal.js b/content_scripts/mode_normal.js index f92f44b8e..8351b2c0f 100644 --- a/content_scripts/mode_normal.js +++ b/content_scripts/mode_normal.js @@ -152,11 +152,13 @@ const NormalModeCommands = { } // Pop path segments. - if (c > 0 && url.pathname != "/") { - url.pathname = url.pathname.split("/").slice(0, -c).join("/"); + if (c > 0 && url.pathname !== "/") { + const initialSegments = url.pathname.substring(1).split("/"); + const segments = initialSegments.slice(0, -c); + url.pathname = `/${segments.join("/")}`; url.search = ""; url.hash = ""; - --c; + c -= initialSegments.length - segments.length; } if (globalThis.location.href !== url.toString()) {