从数组中随机重定向

时间:2013-11-20 15:48:27

标签: javascript php google-chrome-extension hyperlink

/ **  *政治动物  * contentscript.js加载在manifest.json中列出的每个页面上  *此插件用图片替换新闻网站上的所有图像  *穿着西装的动物,作为新闻内容的评论。为Web 2制作  * 2013年11月20日  * /

//随机图像数组

var arrayImg = ['http://www.whattofix.com/images/PoliticalAnimal.jpg','http://www.fubiz.net/wp-content/uploads/2013/03/Fashion-Zoo-Animals26.jpeg','http://img1.etsystatic.com/016/1/7647665/il_340x270.411173311_ojy5.jpg','http://ny-image0.etsy.com/il_fullxfull.85564656.jpg','http://afraidofmice.com/blog/wp-content/uploads/2011/08/berkleyill.jpg','http://elizabethmarshallgalleryblog.files.wordpress.com/2011/05/etsy-panda-for-blog1.jpg','http://moesewco.typepad.com/.a/6a00e5500684b488330120a5c7cf3a970c-300wi','http://ih3.redbubble.net/image.13276877.5059/flat,800x800,070,f.u1.jpg','http://www.tildeshop.com/blog/wp-content/uploads/2012/09/SeaLionFemale-21.jpg'];

//重定向

var acceptedWebsites =['www.cnn.com', 'www.nytimes.com', 'www.latimes.com', 'www.washingtonpost.com', 'www.nbcnews.com', 'www.foxnews.com'];
var currentUrl = document.location.href;
var referrer =  currentUrl.match(/:\/\/(.[^/]+)/)[1];

//确保代码符合我的要求。只要链接显示的数字大于-1,那么站点扩展就可以了     的console.log(引荐);     的console.log(acceptedWebsites.indexOf(引荐));

// var url = acceptedWebsites [Math.floor(Math.random()* acceptedWebsites.length)]; //document.location.href = url;

// image source通过以下脚本函数     $( 'IMG')。每个(函数(){

//创建随机化

var random = arrayImg[Math.floor(Math.random()*arrayImg.length)];
//Takes the current array and applies the source with the random function
$(this).attr('src', random);
//removing the stretch
 var theWidth = $(this).width();
            var theHeight = $(this).height();
            if (theWidth < theHeight) {
                $(this).height(150);
            }
            else {
                $(this).width(150);
            }
});

// alert(“访问以下任何网站:fox.com,nbc.com,nytimes.com,latimes.com或cnn.com”);

我在javascript中有这个数组。我想拥有它,以便用户可以自动重定向到阵列中的一个链接,可能是随机的。我不知道我是否可以在javascript中执行此操作。我正在使用它进行chrome扩展,所以我不知道我是否可以使用php。

除了不断重定向外,这些都是很棒的答案。我想要它,以便它们只是从数组重定向到一次,而不是不断重定向。

**编辑2:我添加了我的整个代码,因为有些东西导致了一个不变的重定向而不只是一次。

**编辑3:我更新了我的代码。 console.log证明我的新变量可以工作并执行== - 1。我如何使用它们重定向?

8 个答案:

答案 0 :(得分:3)

从数组中获取随机网址,然后重定向?

if ( acceptedWebsites.indexOf(document.location.href) == -1 ) {
    var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];
    document.location.href = url;
}

答案 1 :(得分:0)

尝试以下方法:

var acceptedWebsites =['http://www.cnn.com/', 'www.nytimes.com', 'www.latimes.com', 'http://www.washingtonpost.com/', 'http://www.nbcnews.com/', 'http://www.foxnews.com/'];
    var number = Math.floor(Math.random() * acceptedWebsites.length);

数字将生成一个介于1和您的acceptedwebsites数组中的条目数之间的随机数。

答案 2 :(得分:0)

window.location = acceptedWebsites[Math.floor(Math.random() * acceptedWebsites.length)];

答案 3 :(得分:0)

逻辑的基本要点是......

var acceptedWebsites = ['http://www.cnn.com/', 'www.nytimes.com', 'www.latimes.com', 'http://www.washingtonpost.com/', 'http://www.nbcnews.com/', 'http://www.foxnews.com/'];

var randomLink = Math.floor(Math.random() * acceptedWebsites.length);

window.location = acceptedWebsites[randomLink];

答案 4 :(得分:0)

// Get random site
var randomSite = acceptedWebsites[Math.floor(Math.random() * acceptedWebsites.length)];

// redirect to selected site
window.location = randomSite;

答案 5 :(得分:0)

生成“随机”密钥并使用window.location.href重定向用户。其他人发布了相同的方法,但解释不多。我正在尽力让你真正理解这里发生的事情。

请注意,此代码的大部分内容都是注释。它看起来比实际更长。

var acceptedWebsites = ['http://www.cnn.com/', 'www.nytimes.com', 'www.latimes.com', 'http://www.washingtonpost.com/', 'http://www.nbcnews.com/', 'http://www.foxnews.com/'];

// This function returns a random key for an array
function randomKey(arr) {
    // Math.random() returns a number between 0 and 0.99999...
    // If you multiply this value with the length of an array, you get a
    // random floating point number between 0 and that length.
    // Use Math.floor() to round it down to the next integer
    return Math.floor(Math.random() * arr.length);
}

// Select a random website from the array
var key = randomKey(acceptedWebsites);
var newLocation = acceptedWebsites[key];

// Redirect the user
window.location.href = newLocation;

答案 6 :(得分:0)

试试这个解决方案:

var size = acceptedWebsites.length;

var x = Math.floor((Math.random()* size)+1); 

现在使用值为x-1的循环,如

var location = acceptedWebsites[x-1];
window.location.href = location;

如果我们在循环中运行它,我们将在每次0大小的数组之间获得不同的x值,然后我们可以使用该随机值来随机重定向。

答案 7 :(得分:-1)

window.location不起作用,因为内容脚本是非特权的。此外,window.location.href返回当前位置,但它不是方法,因此您无法覆盖它。

你需要:

将重定向网址从内容脚本发送到后台网页:

var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];
chrome.extension.sendRequest({redirect: url });

在后台页面更新标签的网址中会导致重定向:

chrome.extension.onRequest.addListener(function(request, sender) {
    chrome.tabs.update(sender.tab.id, {url: request.redirect});
});