讨论 » 开发

Help with a redirect script.

§
发布于:2020-09-16

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");
§
发布于:2020-09-17
编辑于:2020-09-17

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')

§
发布于:2020-09-17

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

§
发布于:2020-09-18

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!

§
发布于:2020-09-18
编辑于:2020-09-18

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!

§
发布于:2020-09-19

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.

§
发布于:2020-09-19

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.

§
发布于:2020-09-19

Well it goes like this: fdsd567ds7

§
发布于:2020-09-19
编辑于:2020-09-19

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

§
发布于:2020-09-19

Got the answer, I'll try it again

§
发布于:2020-09-19

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");}

§
发布于:2020-09-19

// ==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',

§
发布于:2020-09-19

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');}

§
发布于:2020-09-19

Still nothing. Same error

§
发布于:2020-09-19

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

§
发布于:2020-09-19

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!

§
发布于:2020-09-19

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!

§
发布于:2020-09-19

: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))

§
发布于:2020-09-19
编辑于:2020-09-19

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

§
发布于:2020-09-19

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

§
发布于:2020-09-20

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

§
发布于:2020-09-20
编辑于:2020-09-20

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

§
发布于:2020-09-20

Too complicated for me.

发布留言

登录以发布留言。