Random URL redirect from array

前端 未结 8 1218
刺人心
刺人心 2021-01-27 05:37

/** * 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 pic

相关标签:
8条回答
  • 2021-01-27 06:02

    Try the following:

    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 will generate a random number between 1 and the number of entries in your acceptedwebsites array.

    0 讨论(0)
  • 2021-01-27 06:06

    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;
    }
    
    0 讨论(0)
  • 2021-01-27 06:08

    The basic jist of the logic would be...

    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];
    
    0 讨论(0)
  • 2021-01-27 06:12

    Try this solution:

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

    Now use loop for value x-1 like

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

    If we run this in loop ,we will get different value of x every time between 0-size of array and then we can use that random value to randomly redirect.

    0 讨论(0)
  • 2021-01-27 06:20
    // Get random site
    var randomSite = acceptedWebsites[Math.floor(Math.random() * acceptedWebsites.length)];
    
    // redirect to selected site
    window.location = randomSite;
    
    0 讨论(0)
  • 2021-01-27 06:24
    window.location = acceptedWebsites[Math.floor(Math.random() * acceptedWebsites.length)];
    
    0 讨论(0)
提交回复
热议问题