Reputation: 113
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/...
Upvotes: 1
Views: 2852
Reputation: 3666
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.
Upvotes: 1
Reputation: 24184
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);
}
Upvotes: 0
Reputation: 13880
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);
Upvotes: 1