Is there a way to replace all characters after the last backslash in the currentURL with another string via javascript bookmarklet?
I'm doing a lot of auditing work with Sharepoint sites and having to manually look at the settings pages for sites by entering strings to the end of a URL. For example, I might go to a site like:
https://site.com/..../default.aspx
And I replace the "default.aspx" with "_layouts/user.aspx" and reload the new page so it is now at:
https://site.com/..../_layouts/user.aspx
It's not always "default.aspx", so I can't just use a simple string replace. I know there is a way to manipulate the URL via a javascript bookmarklet, but my knowledge of how to do that is limited at best. Any help or guidance would be greatly appreciated
I don't know if this is what you thought, but if you just want to change the last part of the url with something else, you could use this bookmarklet
javascript:(function(){
var curloc = document.location.href.split('/');
var urlEnding= '/_layouts/user.aspx';
curloc = curloc.splice(0,curloc.length-1).join('/')+urlEnding;
document.location.href = curloc;
})();
You could replace the fixed url with
prompt('Enter your url:', '_layouts/user.aspx');
if you need to change the last part each time.
I hope this helps.
Related
I am trying to write a simple userscript for practice with this concept. Please forgive this example, but its the first one I can think of.
So on sports illistrated swimsuit images, they deliver a smaller than original image. Here is a sample URL: (might be nsfw if you consider bikinis nsfw)
https://www.si.com/.image/c_limit%2Ccs_srgb%2Cq_auto:good%2Cw_385/MTY4MjU5NDM2MzkyMDMyMTI5/chrissy-teigen6jpg.webp
So in order to manipulate the URL to load the original size image, I need to remove this exact string from every image URL I load on this website:
"c_limit%2Ccs_srgb%2Cq_auto:good%2Cw_385/"
Removing that string from the image URL serves the largest image. I am trying to write a userscript to automatically remove that, but I struggling with where to start. I have basics in JS down (I love writing bookmarklets) but am struggling to write a userscript.
Thanks.
Check if the undesirable substring exists in the window.location.href, and if it does, .replace that part with the empty string.
// ==UserScript==
// #name Full SI images
// #include https://www.si.com/*
// #grant none
// ==/UserScript==
if (window.location.href.includes('c_limit')) {
window.location.href = window.location.href.replace('c_limit%2Ccs_srgb%2Cq_auto:good%2Cw_385/', '');
}
To make it more dynamic in terms of coding, you might wanna try to enhance the possibility of passing a not-a-hard-code fixed substring that you would want to remove from the url ("c_limit%2Ccs_srgb%2Cq_auto:good%2Cw_385/") since this might vary from photo to photo, right?. what if we could remove this substring regardless its string content?
The common substring part is c_limit hence we could use this as our starting point to get the rest of the substring after this(including this part) until we find the dimension digits just before the first forward slash after this whole substring on this URL. Check this example out, pal. Hope it helps understand this concept better! Cheers!
let urlStr = "https://swimsuit.si.com/.image/c_limit%2Ccs_srgb%2Cq_auto:good%2Cw_700/MTg5NTA4MDYzMTE0MzA2ODIy/kim_si_domrep_jan2022_s9_01237_updated2_wmweb.webp";
const highResUrlStr = urlStr.replace(/c_limit.*\d[/]/g, "");
console.log(highResUrlStr);
The following code is used to get URL parameters.
<script type="text/javascript">
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
</script>
<script type="text/javascript">
var url = window.location.protocol + "//" + window.location.hostname;
</script>
The page with the above code is accessed from a link on another page. Within that link i'm passing in a news item ID and Title.
When the page with the above code loads the Title in the URL has %20 in place of all spaces.
I've read up on this and found i need to use decodeURI or decodeURIComponent. I've tried to do this in a number of places and alerted the result in the browser but i can't seem to get rid of the %20 from the title within the URL so its obvious i'm not doing it in the right place.
this is my result....
http://PAGE URL HERE/NewsArchive.aspx?Story=New%20site%20launched&ID=17
I believe i somehow need to include a regular expression of /%20/g,"-" in the replace of the parts variable, however i have next to no knowledge of regex.
Could someone let me know what i need to do as i'm drawing a blank. I've seen a number of similar articles but nothing that explains it to my low level of knowledge.
I also post on SharePoint Stack Exchange as this is being used with SharePoint but i haven't had any answers that have worked for me.
Any help appreciated.
I am not sure I understand the question, but it seems like you just want to retrieve the parameters? If you open the developer console (f12 in chrome) on the page with parameters in the URL you can type window.location and see all the properties of the object. If you type window.location.href you can see the full URI. An easy way to separate this is using .split.
window.location.href.split('?')[1] will give you everything after the ? character. You can do decodeUri(window.location.href.split('?')[1]) to get a normal string.
Just keep in mind the arrays returned by split are zero indexed.
Edit in response to the comment:
The technology you're looking for is history.replaceState. More details here. I would highly recommend a thorough skimming.
history.replaceState(null, "/NewsArchive.aspx", "Story=New-site-launched&ID=17")
I have a userscript (http://userscripts.org/scripts/show/179402) that I'm writing that adds a bar to Google Sites like the one they removed. I'm needing the script to take the value of the search field and add it to a url (Replacement URL) and have it replace the url (Original URL) on the bar. In other words, I need to update it where the search term carries over to other pages, like the original google bar they removed.
I've tried this a few different ways. One way I tried was getting the value this way. Which, gets the value fine.
$('#gbqfq').keyup(function() {
var searchterm = this.value;
});
Then I've tried to add the search term to a url that replaces the original URL this way
var url1search = "https://www.google.com/search?q="+searchterm;
$('#url1').attr("href", url1search);
How do you replace a url with a new url plus a variable?
I'm very new to JavaScript, I'm making this script to try to learn it. If someone can help me figure out how to do this I would appreciate it very much.
Ah sorry, I see your problem. searchterm is only defined inside the anonymous function, it would be undefined elsewhere. Try moving rest of the script inside that function too.
I'm trying to use this script that will redirect any webpage to another webpage with a query including the previous webpage's URL.
The code I'm attempting to use looks as such:
window.location = "http://www.readability.com/m?url=" + document.URL;
It's not working though, for what reason I can't figure out. I'm not to familiar with encoding, so this may be the problem. As this works...
window.location = "http://www.readability.com"
But this does not work
window.location = "http://www.readability.com/"
Furthmore I'm working with a mobile browser, not anything like chrome. So it may just be an issue with that, but it may not be. Thanks for reading, and thanks in advance for any help.
You need to escape the URL, otherwise it will be cut on the first & it contains.
window.location = "http://www.readability.com/m?url=" + encodeURIComponent(window.location);
also ignore document.URL and use a more common and interoperable window.location instead.
Don't use escape, since it will not escape all characters you need.You need to escape it with encodeURIComponent:
window.location = "http://www.readability.com/m?url=" + encodeURIComponent(window.location);
We have a javascript function we use to track page stats internally. However, the URLs it reports many times include the page numbers for search results pages which we would rather not be reported. The pages that are reports are of the form:
http://www.test.com/directory1/2
http://www.test.com/directory1/subdirectory1/15
http://www.test.com/directory3/1113
Instead we'd like the above reported as:
http://www.test.com/directory1
http://www.test.com/directory1/subdirectory1
http://www.test.com/directory3
Please note that the numbered 'directory' and 'subdirectory' names above are just for example purposes and that the actual subdirectory names are all different, don't necessarily include numbers at the end of the directory name, and can be many levels deep.
Currently our JavaScript function produces these URLs using the code:
var page = location.hostname+document.location.pathname;
I believe we need to use the JavaScript replace function in combination with some regex but I'm at a complete loss as to what that would look like. Any help would be much appreciated!
Thanks in advance!
I think you want this:
var page = location.href.substring(0,location.href.lastIndexOf("/"));
You can use a regex for this:
document.location.pathname.replace(/\/\d+$/, "");
Unlike substring and lastIndexOf solutions, this will strip off the end of the path if it consists of digits only.
What you can do is find the last index of "/" and then use the substring function.
Not sure you need a regex if you're just pulling off the last slash + content.
http://www.w3schools.com/jsref/jsref_lastIndexOf.asp
I'd probably use that to search for the last "/" character, then do a substring from the start of the string to that index.
How about this:
var page = location.split("/");
page.pop();
page = page.join("/");
I would think you need to use the .htaccess with rewrite rules to change the look of the url, however I am still looking to see if this is available to javascript. Will repost when I find out more
EDIT*
the lastIndexOf would only give you the position, therefor you would still need to replace. ex:
var temp = page.substring(page.lastIndexOf("/"),page.length-1);
page = page.replace(temp, "");
unfortunately I'm not that advanced in my coding so there is probably more efficient coding in the other answers. Sorry for any inconveniences with my initial answer.