数组中的随机URL重定向 [英] Random URL redirect from array
问题描述
* contentscript.js
*这个插件替换了新闻网站上的所有图片作为对新闻已经成为的评论的网站,图片中有
*动物的图片。 Made for Web 2
* 2013年11月20日
* /
//随机图像数组
var arrayImg = ['http://www.whattofix.com/images/PoliticalAnimal.jpg','http://www.fubiz.net/wp-content/上传/ 2013/03 /时尚动物园-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的熊猫换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(referrer);
console.log(acceptedWebsites.indexOf(referrer));
// var url = acceptedWebsites [Math.floor(Math.random()* acceptedWebsites。长度)];
//document.location.href = url;
//图片源经过以下脚本函数
$('img')。 each(function(){
//创建随机化
var random = arrayImg [Math.floor(Math.random()* arrayImg.length)];
//获取当前数组并使用随机函数
(this).attr 'src',random);
//消除延伸
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中做到这一点。我使用这个扩展名,所以我不知道我是否可以使用PHP。
这些都是非常棒的答案,只是它们经常重定向。我想让它们只被重定向到数组中的一次,而不是经常重定向。
**编辑2:我添加了我的整个代码,因为有些东西会导致常量重定向,而不是一次。
**编辑3:我更新了我的代码。 console.log证明我的新变量可以工作并执行== - 1。
从数组中获取一个随机URL并重定向?
$ b如何使用它们重定向?$ b
if(acceptedWebsites.indexOf(document.location.href)== -1){
var url = acceptedWebsites [Math.floor(Math .random()* acceptedWebsites.length)];
document.location.href = url;
}
/** * Political Animals * contentscript.js is loaded on each page(s) listed in manifest.json * This plugin replaces all the images on the website of news sites with pictures of * animals in suits, as a commentary on what the news has become. Made for Web 2 * November 20, 2013 */
//Random Image array
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'];
//redirect
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];
//Making sure the code does what I want it to. As long as the link shows a number greater than -1, then the site extension is working console.log(referrer); console.log(acceptedWebsites.indexOf(referrer));
//var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)]; //document.location.href = url;
// image source goes through the following script function $('img').each(function(){
// creating the randomizing
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 ("Go to any of the follow websites: fox.com, nbc.com, nytimes.com, latimes.com, or cnn.com");
I have this array in javascript. I want to have it so that the user is automatically redirected to one of the links from the array, possibly randomly. I don't know if I can do this in javascript. I am using this for a chrome extension, so I don't know if I can use php.
These are fantastic answers, except they constantly redirect. I want it so that they are just redirected to one from the array once, not constantly redirect.
**Edit 2: I added my whole code because something is causing there to be a constant redirect instead of only once.
**Edit 3: I updated my code. The console.log proves that my new variables work and do ==-1. How can I use them to redirect?
Get a random URL from the array, and redirect ?
if ( acceptedWebsites.indexOf(document.location.href) == -1 ) {
var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];
document.location.href = url;
}
这篇关于数组中的随机URL重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!