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);
Related
I'm currently trying to extract from ReturnUrl= ... I want to extract the URL from the link below using javascript. Can anyone help?
http://testdealbuilderCCMS/questionnaire.aspx?db_template_reference=Construction: Westfield Services Agreement&ContractDescription=Facilities Contract&NatureServices=FACILITIES&SiteDescription=Retail Units&ThirdPartyAgreementsList=&ServiceFee=1000&ReturnUrl=http://localhost:4965&launcher.aspx?directLink=PX&caseKey=7ccef65756504a79bc3a4a6687c0d9555e519ec9079241c9944c6a523704&PXid=
there are lots of edge cases here that will make this fail. So be careful, use this only if your string always ends with the ReturnURL parameter.
Find the position of the ReturnURL= in the string then get the substring from ReturnURL= position + ReturnURL= length, to the end.
http://jsfiddle.net/3hvajedg/1/
the_string = 'http://testdealbuilderCCMS/questionnaire.aspx?db_template_reference=Construction: Westfield Services Agreement&ContractDescription=Facilities Contract&NatureServices=FACILITIES&SiteDescription=Retail Units&ThirdPartyAgreementsList=&ServiceFee=1000&ReturnUrl=http://localhost:4965&launcher.aspx?directLink=PX&caseKey=7ccef65756504a79bc3a4a6687c0d9555e519ec9079241c9944c6a523704&PXid=';
alert(the_string.substring((the_string.indexOf('ReturnUrl=')+'ReturnUrl='.length)));
I have no script abilitiy, but i'd like to edit an existing script which is currently restricting the script from running on any page other then the one that has a certain string in the URL.
Here is the snippet of the script which limits it from running
if(location.href.indexOf("MODULE=MESSAGE")>0||location.href.indexOf("/message")>0)
This only allows the script to run on these pages
mysite/2014/home/11609?MODULE=MESSAGE1
and the pages range from Message1 to Message20
mysite/2014/home/11609?MODULE=MESSAGE20
I would like to also allow the script to be loaded and ran on these pages
mysite/2014/options?L=11609&O=247&SEQNO=1&PRINTER=1
where the SEQNO=1 ranges from 1 to SEQNO=20, just like the MESSAGE1-MESSAGE20 do
Can someone show me how i can edit that small snippet of script to allow the SEQNO string found in the url to work also.
Thanks
If you can't just remove the condition altogether (there's not enough context to know if that's an option), you can just add another or condition (||) like so:
if(location.href.indexOf("MODULE=MESSAGE")>0
||location.href.indexOf("/message")>0
||location.href.indexOf("SEQNO=")>0)
Note that the second clause there isn't actually being used in any of your examples, so could potentially be removed. Also note that this isn't actually checking for a number so it isn't restricted to Message1 to Message20 as you suggest. It would match Message21 or even MessageFoo. That may or may not be a problem for you. You can make the conditions as restrictive or as lose as makes sense.
If you just want to check for the existence of "SEQNO", simply duplicate what is being done for "MODULE_MESSAGE".
if(location.href.indexOf("MODULE=MESSAGE")>0 ||
location.href.indexOf("SEQNO=")>0 ||
location.href.indexOf("/message")>0)
If you want to also ensure that "MESSAGE" ends in 1-20, and "SEQNO=" ends in 1-20, you can use a regex.
// create the end part of the regex, which checks for numbers 1-20
var regexEnd = "([1-9]|1[0-9]|20)[^0-9]*$";
// create the individual regexes
var messageRegex = new RegExp("MODULE=MESSAGE" + regexEnd);
var seqnoRegex = new RegExp("SEQNO=" + regexEnd);
// now comes your if statement, using the regex test() function, which returns true if it matches
if(messageRegex.test(location.href) ||
seqnoRegex.test(location.href) ||
location.href.indexOf("/message")>0)
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.
I'm doing a JavaScript plugin, launched at every page-load, that replaces every matching structure with a link... That link redirects to a web application/database. A resource for coders of the Mount&Blade game.
In theory is easy, but I've found an huge obstacle in my way to the success: Regular expressions.
Even helped by a program named QuickRegex I can't get the structure to match. Or if I don't do a proper conditioning it outputs wrong results. The matching structure is as follows:
(item_set_slot, "itm_heavy_crossbow", slot_item_multiplayer_item_class),
I want to pick item_set_slot and turn it into a link to http://mbcommands.ollclan.eu/#$1
This is the code I'm using, that works, more or less. ;)
/* Mount&Blade Command Database Linking by Swyter */
function swymbcommandshooker(){
/* Regular HTML Expressions */
document.getElementsByTagName("body")[0].innerHTML=document.getElementsByTagName("body")[0].innerHTML.replace(/[\(]([a-zA-Z_]+)[\,]/gi, "(<a href='http://mbcommands.ollclan.eu/#$1' title='[?] Take an look in the Command Database' target='_blank'>$1</a>,");
/* Python highlighter Support...*/
document.getElementsByTagName("body")[0].innerHTML=document.getElementsByTagName("body")[0].innerHTML.replace(/(</span>([_a-z]+)\,/gi, "(</span><a href='http://mbcommands.ollclan.eu/#$1' title='[?] Take an look in the Command Database' target='_blank'>$1</a>,");
}
addOnloadHook( swymbcommandshooker );
Thanks in advance.
Hm, I'm not sure if I have understand you correctly, but if you really just want the match "item_set_slot" in "(item_set_slot, "itm_heavy_crossbow", slot_item_multiplayer_item_class)," the following regex should do:
/^\(([a-z_]+),/i
The JavaScript to generate the URL could look like this:
var tuple = '(item_set_slot, "itm_heavy_crossbow", slot_item_multiplayer_item_class),';
var url = tuple.replace(/^\(([a-z_]+),.*/i, 'http://mbcommands.ollclan.eu/#$1');
Note the appended .* in the regex, which is needed to match the rest of the tuple.
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.