从数组随机重定向URL
Random URL redirect from array
/***政治动物*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(referrer);console.log(acceptedWebsites.indexOf(referrer));
//var url=acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];//document.location.href=url;
//图像源通过以下脚本函数$('img').each(function(){
//创建随机化
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证明了我的新变量是有效的并且do==-1。如何使用它们重定向?
从数组中获取一个随机URL,然后重定向?
if ( acceptedWebsites.indexOf(document.location.href) == -1 ) {
var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];
document.location.href = url;
}
尝试以下操作:
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);
number将生成一个介于1和您接受的网站数组中的条目数之间的随机数。
window.location = acceptedWebsites[Math.floor(Math.random() * acceptedWebsites.length)];
逻辑的基本jist是…
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];
// Get random site
var randomSite = acceptedWebsites[Math.floor(Math.random() * acceptedWebsites.length)];
// redirect to selected site
window.location = randomSite;
生成一个"随机"密钥并使用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;
试试这个解决方案:
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值,然后我们可以使用该随机值来随机重定向。
window.location不起作用,因为内容脚本没有特权。此外,window.location.href返回当前位置,但它不是一个方法,因此无法覆盖它。
你需要:
将重定向url从内容脚本发送到后台页面:
var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];
chrome.extension.sendRequest({redirect: url });
在后台页面更新选项卡的url,这将导致重定向:
chrome.extension.onRequest.addListener(function(request, sender) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});
- 我的URL重定向安全吗
- 基于屏幕宽度的URL重定向
- HTML5/Javascript验证复选框提交URL重定向
- 基于变量的Javascript URL重定向无法正常工作
- 将使用 history.pushState() 创建的 URL 重定向到基本 URL
- 通过随机 url 重定向传递和添加参数
- 将 URL 重定向到子文件夹
- 如何使用jQuery根据引用/当前URL重定向页面
- Firefox 插件中的 OnBeforeRequest URL 重定向(从 Chrome 扩展程序转换)
- 带有 JavaScript URL 重定向的正则表达式
- 使用 JavaScript 使用 MD5 值进行 URL 重定向
- 当网址路径中有哈希时,Javascript url 重定向
- Angular.js:prettyPhoto URL重定向到Angular路由中提到的默认页面
- 将特定URL重定向到moblile上的另一个URL
- 使用javascript或jquery修改href-或-URL重定向/重写和模式匹配
- 从主页url重定向到url+锚点
- 如何在URL重定向后获得新的URL
- 正在获取另一个URL重定向到的URL
- 循环中的URL重定向
- 如何从一个URL重定向到另一个URL