1

I've a userscript that redirects to a random site from a list. (Reference: How to redirect to one out of given set of sites?)

How do I get the script to add a random suffix to select sites, like wallbase.cc?

Here's my script so far:

// ==UserScript==
// @name        Multipage, MultiSite slideshow of sorts
// @match       http://*.breaktaker.com/*
// @match       http://*.imageshack.us/*
// @match       http://static.tumblr.com/*
// @match       http://withfriendship.com/images/*
// @match       http://failjudge.com/*
// @match       http://wallbase.cc/*
// ==/UserScript==

var urlsToLoad  = [
    'http://www.breaktaker.com/albums/pictures/animals/BigCat.jpg'
    , 'http://img375.imageshack.us/img375/8105/bigcats34ye4.jpg'
    , 'http://withfriendship.com/images/g/33769/1.jpg'
    , 'http://static.tumblr.com/yd0wcto/LXQlx109d/bigcats.jpg'
    , 'http://failjudge.com/'
    , 'http://wallbase.cc/wallpaper/ + [random number suffix from 1- 10000000000]'
];

setTimeout (GotoRandomURL, 4000);

function GotoRandomURL () {
    var numUrls     = urlsToLoad.length;
    var urlIdx      = urlsToLoad.indexOf (location.href);
    if (urlIdx >= 0) {
        urlsToLoad.splice (urlIdx, 1);
        numUrls--;
    }

    urlIdx          = Math.floor (Math.random () * numUrls);
    location.href   = urlsToLoad[urlIdx];
}
Community
  • 1
  • 1
adi
  • 207
  • 1
  • 10

1 Answers1

1

Adding the random suffix, means that the script must change how it checks what site it is on -- as well as how it selects a new URL. So, for those sites, we must check for a partial match.

But, rather than explain every piece of it, here's a big bunch o' code. ;)   It should be a little self-documenting. :

// ==UserScript==
// @name        Multipage, MultiSite slideshow of sorts
// @match       http://*.breaktaker.com/*
// @match       http://*.imageshack.us/*
// @match       http://static.tumblr.com/*
// @match       http://withfriendship.com/images/*
// @match       http://failjudge.com/*
// @match       http://wallbase.cc/*
// ==/UserScript==

var urlsToLoad  = [
    { url:          'http://www.breaktaker.com/albums/pictures/animals/BigCat.jpg',
      useSuffix:    false
    },
    { url:          'http://img375.imageshack.us/img375/8105/bigcats34ye4.jpg',
      useSuffix:    false
    },
    { url:          'http://withfriendship.com/images/g/33769/1.jpg',
      useSuffix:    false
    },
    { url:          'http://static.tumblr.com/yd0wcto/LXQlx109d/bigcats.jpg',
      useSuffix:    false
    },
    { url:          'http://failjudge.com/',
      useSuffix:    false
    },
    //--- Fur suffix URLs, include everything before the suffix only.
    { url:          'http://wallbase.cc/wallpaper/',
      useSuffix:    true
    }
];

/*--- Since many of these sites load large pictures, Chrome's normal or its
    "document end" injection may fire a good deal before the image(s) finish
    loading.
    So, insure script fires after load:
*/
window.addEventListener (
    "load",
    function () { setTimeout (GotoRandomURL, 4000); },
    false
);
if (document.readyState == "complete") {
    setTimeout (GotoRandomURL, 4000);
}


function GotoRandomURL () {
    var numUrls     = urlsToLoad.length;
    var urlIdx      = -1;

    for (var J = numUrls - 1;  J >= 0;  --J) {
        if (urlsToLoad[J].useSuffix) {
            //--- Check that URL starts with the specified value
            var prefChk = new RegExp ('^' + urlsToLoad[J].url, 'i');
            if (prefChk.test (location.href) ) {
                urlIdx  = J;
                break;
            }
        }
        else {
            if (urlsToLoad[J].url  ==  location.href) {
                urlIdx  = J;
                break;
            }
        }
    }

    if (urlIdx >= 0) {
        urlsToLoad.splice (urlIdx, 1);
        numUrls--;
    }

    urlIdx          = Math.floor (Math.random () * numUrls);
    var targURL     = urlsToLoad[urlIdx].url;

    if (urlsToLoad[urlIdx].useSuffix) {
        //--- Note:  wallbase.cc currently has less than 2-million wallpapers.
        targURL    += Math.ceil (Math.random () * 2000000);
    }                                              
    console.log ('\n\n***\n', targURL, '\n***\n\n');
    location.href   = targURL;
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • awesome man the addition of the thingie which makes the script work only when page fully loads is wowsome. i think u've just created a breakthrough script that can help many users. – adi Jul 10 '12 at 12:58
  • please dont mind if i further asks questions to u regarding the more awesomefication of it. 1] we have wallbase.cc in the script and a limit of suffix from 1-2 million is added for it .but if have more sites like wallbase.cc which dont have 2 million wallies but just 2 thousand . so can we set special branch[suffix set from 1-2000] of script for that site. – adi Jul 10 '12 at 13:00
  • 2] some sites used names instead of numbers as suffix , for ex: 'http://piccsy.com/2012/07/love-quotes-pics-6menuwhgo' ,so is there some backward url for such links .otherwise i know that its impossible to make it work on such sites .backward address like 111.22.34.244 for xyz.com. finally , bro , thanx for the hardwork and care , dont know its hard or easy for you , but for me man this all stuff sure needs lots of brains n time. :) – adi Jul 10 '12 at 13:00
  • 1
    Breakthrough script? (^_^) If it helps somebody, great! For sites with different random ranges, you would add a new member to the `urlsToLoad` entry for that site like so: `{ url: 'SITE_URL', useSuffix: true, maxSuff: 2000 }` then change the one line to: `targURL += Math.ceil (Math.random () * urlsToLoad[urlIdx].maxSuff);`. ... Sites like piccsy.com are quite a bit more complicated unless you know the full URL for each pic. For something like that, open a new question. – Brock Adams Jul 10 '12 at 20:37