Skip to content

Add goUp options to pop URL anchor/fragment/hash & query string #4718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions background_scripts/all_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ const allCommands = [
desc: "Go up the URL hierarchy",
group: "navigation",
advanced: true,
options: {
popAnchor: "Remove the anchor/fragment/hash from the URL, if present.",
popQuery: "Remove query parameters from the URL, if present.",
},
},

{
Expand Down
36 changes: 27 additions & 9 deletions content_scripts/mode_normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,35 @@ const NormalModeCommands = {
},

// Url manipulation.
goUp(count) {
let url = globalThis.location.href;
if (url.endsWith("/")) {
url = url.substring(0, url.length - 1);
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;
}

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("/");
// Pop query params.
if (c > 0 && registryEntry.options.popQuery && url.search !== "") {
url.search = "";
url.hash = "";
--c;
}

// Pop path segments.
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 -= initialSegments.length - segments.length;
}

if (globalThis.location.href !== url.toString()) {
globalThis.location.href = url.toString();
}
},

Expand Down