Greasy Fork is available in English.

Diskussionen » Entwicklung

Help with a redirect script.

§
Veröffentlicht: 16.09.2020

Hello all, need help with a redirect script I want to make. Here is the thing, I have about 20 sites with (obviously different addresses) but with the same pattern of functionality. They work the same.

// ==UserScript==
// @name        Redirect
// @include     https://example1.com/sub*
// ==/UserScript==

window.location.replace("https://example1.com/redirected");

This one example works nicely, but I need another 19 sites to add and I don't know how. I didn't try this, but I'm sure this will not work:

// ==UserScript==
// @name        Redirect
// @include     https://example1.com/sub*
// @include     https://example2.com/sub*
// @include     https://example3.com/sub*
// @include     https://example4.com/sub*
// ==/UserScript==

window.location.replace("https://example1.com/redirected");

window.location.replace("https://example2.com/redirected");

window.location.replace("https://example3.com/redirected");

window.location.replace("https://example4.com/redirected");
§
Veröffentlicht: 17.09.2020
Bearbeitet: 17.09.2020

If you don't want to redirect all of the @include links to the same url, but only a few of them you can do this

if ((top.location.host === 'example1.com/sub') || (top.location.host === 'example2.com/sub') || (top.location.host === 'example3.com/sub')) /*Make sure to run on the correct websites only */ {window.location.assign("https://google.com");} // Finishes the if condition

Or make a condition for every single one of them, like
if ((top.location.host === 'example1.com/sub')) {window.location.replace("https://example1.com/redirected");}

if ((top.location.host === 'example2.com/sub')) {window.location.replace("https://example2.com/redirected");}

if ((top.location.host === 'example3.com/sub')) {window.location.replace("https://example3.com/redirected");}

if ((top.location.host === 'example4.com/sub')) {window.location.replace("https://example4.com/redirected");}

Instead of top.location.host, you can use location.href, using location.href means that you will need to add the whole url to the condition statement, like...
if ((location.href === 'https://example1.com/sub')) {window.location.replace("https://example4.com/redirected");}

But if there's anything after sub in the url the script won't do anything, then you will need an regex condition like in this example location.href.match('https:\/{2}myanimelist.net\/(?:[^\/]+\/){3}forum')

§
Veröffentlicht: 17.09.2020

Thank you very much. I'll try to put it together tomorrow and I'll let you know.

§
Veröffentlicht: 18.09.2020

Non of this don't work. I guess I'll do 20 separate scripts, 1 for each site like this:

// ==UserScript==
// @name        Redirect
// @include     https://example1.com/sub*
// ==/UserScript==

window.location.replace("https://example1.com/redirected");

At least it works. Thank you anyway!

§
Veröffentlicht: 18.09.2020
Bearbeitet: 18.09.2020

That works too, but isn't really smart. You had better make a condition for every single one of them, like
if ((top.location.host === 'example1.com/sub')) {window.location.replace("https://example1.com/redirected");}

if ((top.location.host === 'example2.com/sub')) {window.location.replace("https://example2.com/redirected");}

Give me the correct urls that I can help you more


You are welcome!

§
Veröffentlicht: 19.09.2020

Yes, I did this. But if I put it like you say:
if ((top.location.host === 'example1.com/sub')) {window.location.replace("https://example1.com/redirected");}
it doesn't work, because in the original request there is the asterisk example1.com/sub*, iit continues with the random numbers each time they are different. So when I tried to make it like this:
if ((top.location.host === 'example1.com/sub*')) {window.location.replace("https://example1.com/redirected");}
it didn't work either.

§
Veröffentlicht: 19.09.2020

That's why I said
"But if there's anything after sub in the url the script won't do anything, then you will need an regex condition like in this example location.href.match('https:\/{2}myanimelist.net\/(?:[^\/]+\/){3}forum')"

open https://webchat.freenode.net/#regex and ask them to make the regex for you, you need to give some examples of the url to them,so they can make the regex.

§
Veröffentlicht: 19.09.2020

Well it goes like this: fdsd567ds7

§
Veröffentlicht: 19.09.2020
Bearbeitet: 19.09.2020

open https://webchat.freenode.net/#regex and ask them to make the regex for you, you need to give some examples of the FULL url to them,so they can make the regex.

You don't need to create an account to talk with them

§
Veröffentlicht: 19.09.2020

Got the answer, I'll try it again

§
Veröffentlicht: 19.09.2020

Ok, did this but it doesn't work

// ==UserScript==
// @version 1
// @grant none
// @include https://something.io/here/*
// @include https://another.com/here/*
// ==/UserScript==


if ((location.href === 'https://something.io/here/^[a-z\d]+$')) {window.location.replace("https://something.io/there");}
if ((location.href === 'https://another.com/here/^[a-z\d]+$')) {window.location.replace("https://another.com/there");}

§
Veröffentlicht: 19.09.2020

// ==UserScript==
// @include https://something.io/here/*
// @include https://another.com/here/*
// ==/UserScript==


if ((location.href.match === 'https://something.io/here/^[a-z\d]+$')) {window.location.replace('https://something.io/there');}
if ((location.href.match === 'https://another.com/here/^[a-z\d]+$')) {window.location.replace('https://another.com/there');}



Fixed location.href.match and quotes - still nothing.

Debugger says console.error('Script error in %s:\n%s: %s',

§
Veröffentlicht: 19.09.2020

This is the correct usage

if (location.href.match('https://something.io/here/^[a-z\d]+$')) {window.location.replace('https://something.io/there');}
if (location.href.match('https://another.com/here/^[a-z\d]+$')) {window.location.replace('https://another.com/there');}

§
Veröffentlicht: 19.09.2020

Still nothing. Same error

§
Veröffentlicht: 19.09.2020

the problem is the regex
^[a-z\d]+$

go back to that website and say that you want to use that with this example location.href.match('https://another.com/here/^[a-z\d]+$') in javascript
tell them that ^ and +$ should be escaped,otherwise the script won't work

§
Veröffentlicht: 19.09.2020

It woooooooorks! Finally, thank you VERY VERY MUCH! Thank you for your time and help, you're the best!!!
They said it should be like this [a-z\d]+ instead of this ^[a-z\d]+$
Anyway, you just saved me countless absolutely useless clicks! Thank you again!

§
Veröffentlicht: 19.09.2020

Yeah, when I tested your code the script worked just with [a-z\d], but I wasn't sure if this would cause some problems/bugs or if it would completely stop working on the website you are using, so I though that was better to talk with them.

I told you haha..., you are welcome!

§
Veröffentlicht: 19.09.2020

:o))) I was thinking, maybe there is a simple way to add a timeout before each redirect, cuz now it redirects imidiatelly which is suspicios. Even now, when I open all those sites at once manually, it want to confirm that I'm not a robot, which I'm obviously not:o))

§
Veröffentlicht: 19.09.2020
Bearbeitet: 19.09.2020

if (location.href.match('https://something.io/here/[a-z\d]+')) {
setTimeout(function(){ window.location.replace('https://something.io/there'); }, 5000); //Redirect after 5 secs
}

// 10000 Redirect after 10 secs

§
Veröffentlicht: 19.09.2020

You ARE GEENIEUUUS!!! Thank you, I'll try it tomorrow and I'll let you know.

§
Veröffentlicht: 20.09.2020

Is there an easy way to make this timeout random? Like from 2000 to 7000?

§
Veröffentlicht: 20.09.2020
Bearbeitet: 20.09.2020

The codes are bigger and I don't think it's worth to do it.

But you can research https://www.sitepoint.com/community/t/how-to-set-function-to-random-delay-time-on-first-page-load/276275/2

I tried some stuff but they usually don't pass 11 seconds, I'm not sure why, so I give up.

Good luck

§
Veröffentlicht: 20.09.2020

Too complicated for me.

Antwort schreiben

Anmelden um eine Antwort zu senden.