如何让我的重定向脚本为选择的站点添加随机后缀? [英] How do I get my redirecting script to add a random suffix to select sites?

查看:80
本文介绍了如何让我的重定向脚本为选择的站点添加随机后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从列表中重定向到随机站点的用户脚本.(参考:如何重定向到一个给定的一组网站?)

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

如何让脚本添加随机后缀以选择网站,例如 wallbase.cc?

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

这是我目前的脚本:

// ==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];
}

推荐答案

添加随机后缀意味着脚本必须更改它检查它所在站点的方式——以及它选择新 URL 的方式.因此,对于这些网站,我们必须检查部分匹配.

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

这篇关于如何让我的重定向脚本为选择的站点添加随机后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆