Last active
August 21, 2020 01:21
-
-
Save ychin/5173a094de3148e287e4e5c964c0f135 to your computer and use it in GitHub Desktop.
Bookmarklet – Go up one directory in URL path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function(num) { | |
/* | |
* Goes up one or more directory in path or remove path hash or query. | |
* To use this, condense into oneline then, paste the string into a | |
* bookmark in a web browser. | |
* Replace the "1" at the end of the script to change how many levels to go up. | |
*/ | |
var newUrl = new URL(window.location); | |
for (var i = 0; i < num; i++) { | |
if (newUrl.hash != '') { | |
newUrl = new URL(newUrl.protocol + '//' + newUrl.host + newUrl.pathname + newUrl.search); | |
} | |
else if (newUrl.search != '') { | |
newUrl = new URL(newUrl.protocol + '//' + newUrl.host + newUrl.pathname); | |
} | |
else { | |
var slashIndex = newUrl.pathname.lastIndexOf('/', newUrl.pathname.length - 2); | |
if (slashIndex != -1) { | |
newUrl.pathname = newUrl.pathname.slice(0,slashIndex); | |
} | |
else if (newUrl.pathname != '/' && newUrl.pathname != '') { | |
newUrl.pathname = ''; | |
} | |
else { | |
var dotIndex = newUrl.host.indexOf('.'); | |
if (dotIndex != -1) { | |
newUrl.host = newUrl.host.slice(dotIndex + 1); | |
} | |
} | |
} | |
} | |
window.location = newUrl; | |
})(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment