1

I'm trying to write an userscript for a forum wherein, I require it to navigate to different URLs randomly.

The basic idea is, I want to be online most of the time to increment my online spent time. The forum will not increment to online spent time after some time of inactivity(Let's assume 5 mins).

So, how can I write a javascript code to keep navigating to different URLs within the forum.

This is the format of forum threads : somesite.xxx/showthread.php?tid=xxxxxxx So the script should visit some random thread (say 0123456), wait for 5 mins and visit next random thread (say 1123456) and keep this loop repeating.

I tried this :

setTimeout(function() {
    location.href = "somesite.xxx/showthread.php?tid=4128749";
}, 10000);

setTimeout(function() {
    location.href = "somesite.xxx/showthread.php?tid=5128749";
}, 10000);

setTimeout(function() {
    location.href = "somesite.xxx/showthread.php?tid=3128749";
}, 10000);

But I cannot keep adding all the countless URLs and also the above code isn't working, keeps appending the site URL like somesite.xxx/somesite.xxx/...

3
  • You could try like this to redirect to another url // similar behavior as an HTTP redirect window.location.replace("http://stackoverflow.com"); // similar behavior as clicking on a link window.location.href = "http://stackoverflow.com"; Commented Mar 14, 2018 at 3:01
  • But I cannot keep adding all the countless URLs get a list from some server somewhere Commented Mar 14, 2018 at 3:03
  • There are various ways you 'could' achieve this. It just doesn't sound like a good idea though. The site may have good reasons for tracking legitimate usage, and you're trying to circumvent this. Commented Mar 14, 2018 at 3:30

3 Answers 3

1

Well, you need to generate the list of URLs somehow. Whether that's from an API, a PHP or JavaScript array, text file, whatever. Otherwise you have to generate a random 7 digit number and hope for a hit.

Once you get your list of threads, turn them into an array, and then pick a random one, and then run your setTimeout function (which is missing window. before location)

var threads = ['4128749', '5128749', '3128749', ...];  
var random  = threads[Math.floor(Math.random() * threads.length)];

setTimeout(function(){
    window.location.href = 'http://somesite.xxx/showthread.php?tid=' + random;
}, 10000);

If you don't care about misses, or you know all 7 digit numbers will result in a thread, you can just generate the number randomly and then set the location to that.

//Generate random 7 digit number
var random  = Math.floor(Math.random()*9000000) + 1000000;

setTimeout(function(){
    window.location.href = 'http://somesite.xxx/showthread.php?tid=' + random;
}, 10000);
Sign up to request clarification or add additional context in comments.

Comments

1

Use setInterval instead of setTimeout as it executes the function passed as an argument repeatedly.

const timeout = 300000;

setInterval(function () {
    const sevenRandom = Math.floor(100000 + Math.random() * 9000000);
    const windowHandle = window.open('somesite.xxx/showthread.php?tid=' + sevenRandom, '_blank');

    setTimeout(function () {
        windowHandle.close();
    }, timeout);
}, timeout);

Paste above snippet in console of your browser and it will open your URL in new tab after every five minutes. Also it will close the previously opened tab to avoid lots of tab being opened.

Note: Avoid using this for any kind of spam and only for genuine purpose.

Comments

0

Say, you have an array of hrefs. e.g. hrefArray = [href1, href2, ...]. Now you can follow loop through the hrefs.

const hrefArray = ['somesite.xxx/showthread.php?tid=4128749', 'somesite.xxx/showthread.php?tid=5128749'];
const len = hrefArray.length;

for (let i = 0; i < len; i++) {
  setTimeout(function() {
    location.href = hrefArray[i];
  }, (Math.floor(Math.random() * i) + 1000);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.