Greasy Fork is available in English.

Discussioni » Richieste di creazione

Replace string in an URL

§
Pubblicato: 24/04/2016

Replace string in an URL

Hey,

I want to replace a part of an URL when loading. It works for example for google->bing with the following code:
location.href = location.href.replace("google", "bing");
But for example with https://w0bm.com/6065 to https://w0bm.com/5548 it is not working
location.href = location.href.replace("6065", "5548");

Has this to do with the way the page is loading? Any suggestions how to solve this? :/

Greets René

§
Pubblicato: 24/04/2016

Hi René,
while the replacement works all right I came across the strange side-effect that the comanded re-location turns into an endless loop.
Embraced by a condition it removes that effect and maybe it might solve your issue too, as it will only perform if the location is true. Try:

if (location.href.match("6065")) {
	location.href = location.href.replace("6065", "5548");
}

I think the url is about the first response one gets and the following contents of the page should not matter for immediate re-location.
Maybe there's a problem with the meta-data of the script wherein you're useing this redirect or other scripts that also run for this hoster. (?)

greets ~ Vivre

§
Pubblicato: 25/04/2016

Hi Vivre,
thanks for your help. Unfortunately it's not working.

I tried the following and nothing happened. So Chrome/Tampermonkey isn't getting the link at all?

if (location.href.match("6065")) {
alert("does it work?");
}

Greets René

FFW
§
Pubblicato: 25/04/2016

Works as it should here. Opera and TM BETA.

§
Pubblicato: 25/04/2016

Sorry to hear René,
and it apears rather strange to me and I hope someone with Chrome/Tampermonkey gives it a try for you too.

However I was wondering if you'd get any kind of resonse at all so I thought of the following:

window.onload = alert(location.href +'\n'+ window.location.href);

Testing it myself I also came across sides where I got several alerts - and not all of them seemed to be due to hidden iframes - an interesting experience.

And another thought: could your location-check be nested inside a function that has become obsolet or none-funtioning, or is placed behind an error, so that it's not reached for processing anymore.(?)

cheers ~ Vivre

§
Pubblicato: 26/04/2016

Thanks again for your help, Vivre (and Jenie for testing),

I tried "window.onload = alert(location.href +'\n'+ window.location.href);" on several pages. And only on www.google.de I got a response (twice https://www.google.de/?gws_rd=ssl).

And right now I am only running one script in Tampermonkey which in total is:

// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.google.de/?gws_rd=ssl
// @grant none
// ==/UserScript==

if (location.href.match("6065")) {
location.href = location.href.replace("6065", "5548");
}

So the latter is also probably not true. :/

I also tried TM in Opera now and there nothing happens at all. No matter what command I use, nothing happens.

Greets René

FFW
§
Pubblicato: 26/04/2016

Change the // @match line to // @include * to have it working on every page.

And run the following line in the script to check in general.. location.href.includes('http') ? alert('Works') : alert('Does not work');

§
Pubblicato: 26/04/2016
Modificato: 26/04/2016

Hi hi :smiley:
now it's obvious.

In the script-header the @match sets the targets where the given script shall run. In your case it's only on ...google.de/?gws_rd=ssl and nowhere else.

If you e.g. used @include * the script would run on any page.
To make it work on ...w0bm.com/6065 you'd need to add: @match https://w0bm.com/6065

Now in Greasemonkey I can easily change the match-contains (aka include / exclude) via Greasemonkey-menu: 'manage scripts'. One can include or exclude specific pages or alter existing entries. It's likely similar doable with Tampermonkey.

I made a kind of template for you with 3 examples. It runs on any page but only processes those commands where the URL-condition is met.
I'm useing such a kind of script for a handful of pages where I only want to influence a little thing on each page without having to install many scripts (one for each page). Besides it's handy to have for quick testings.

maybe this is handy for you too ~ Vivre

// ==UserScript==
// @name Multi-script
// @namespace test_as_can
// @version 0.1
// @description Take over the world!
// @author We
// @include *
// @grant none
// ==/UserScript==

if (window.top !== window.self)	   // Don’t run in frames.
	return;

var currentURL = location.href;


// ### https://w0bm.com/...

if (currentURL.match("w0bm.com/6065")) {
	location.href = location.href.replace("6065", "5548");
};


// ### https://www.google...

if (currentURL.match("google.de")) {
	location.href = location.href.replace("google", "bing");
};


// ### https://greasyfork.org/...

if (currentURL.match("greasyfork.org")) {
	if (currentURL.match("scripts")) {
		alert("Scripting makes fun :-)))");
	} else {
		alert("Try to take over the world! ;-)");
	};
};


// end of script
§
Pubblicato: 26/04/2016
Modificato: 27/04/2016

a tip: when you start writing a new script, the first line of code should be
console.log('my script started..')

§
Pubblicato: 27/04/2016

Thank you, everyone. It works. I really need to be more careful when copying and modifying other scripts :D

Pubblica risposta

Accedi per pubblicare una risposta.