如何从各种选择中获取javascript选择随机选项?

时间:2013-06-17 23:35:27

标签: javascript random greasemonkey

我想用油脂猴子的程序来代替着名诗人的名字。该程序应该随机执行此操作。

// ==UserScript==
// @name          Profanity Filter - Quantum Remix
// @namespace     kornk/skidfilter
// @description Filters words 
// @include         *
// ==/UserScript==

/*

  Author: MCE - Edited by aaa


 Version: 1.0
 1.0 - First Release

 Competing scripts and extensions:
 * http://www.arantius.com/article/arantius/clean+language/

  Improvements Needed:


   */

  // Licensed for unlimited modification and redistribution as long as
  // this notice is kept intact.

 (function() {

//some performance settings
var MillisecondsPauseBetweenBatches=3;
var NodesPerBatch = 20;
var ReplacementText = "TBD";

//edit the words here
//sorted alpha backwords to catch bad word groupings
var badwords=['shit', 'fuck', 'cunt', 'dick', 'dicks', 'penis', 'vagina', 'whore', 'fromatic', 'ass', 'asses', 'shits', 'fucks', 'fucker', 'fuckers', 'motherfucker', 'motherfuck', 'motherfucking', 'fucking', 'motherfuckers', 'nigger', 'niggers', 'gay', 'gays', 'homo', 'homos', 'vaginas', 'pussy', 'whores', 'shits', 'shitter', 'anus', 'arse', 'arsehole', 'asshat', 'ass-hat', 'assbag', 'douche', 'assbite', 'asscock', 'asses', 'assface', 'asshole', 'asshead', 'assnigger', 'assshole', 'asswipe', 'bampot', 'bastard', 'beaner', 'bitch', 'bitchass', 'bitches', 'bitchtits', 'bitchy', 'blow job', 'blowjob', 'bollocks', 'bollox', 'boner', 'bullshit', 'buttplug', 'butt plug', 'buttfucker', 'chink', 'choad', 'chode', 'clit', 'clitface', 'clitfuck', 'cock', 'cockass', 'cockbite', 'cockburger', 'cockface', 'cum', 'cumdumpster', 'cumguzzler', 'cuntface', 'cuntmuffin', 'damn', 'dickbag', 'dickface', 'dickmilk', 'dike', 'dildo', 'doochbag', 'douchebag', 'dumbass', 'dumbshit', 'dyke', 'fag', 'fagbag', 'faggot', 'fuckbag', 'fucker', 'fuckhead', 'fucking', 'fucken', 'fucktard', 'gooch', 'hoe', 'jizz', 'mothafucka', 'mothefuckas', 'mothafucking', 'motherfuckin', 'motherfucken', 'piss', 'queer', 'tit', 'boob', 'twat', 'wanker'];

var i = 0;
var st = new Date().valueOf();  //for performance testing   
var els = document.evaluate('//text()', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var bw="\\b("+badwords.join("|")+")\\b";
bw=new RegExp(bw, "gi");

//do the title first
document.title=document.title.replace(bw, ReplacementText);

function CleanSome() 
{       
    var el;
    var newval="";
    var data = "";
    var loopCount = 0;
    while ((el=els.snapshotItem(i++)) && (loopCount <= NodesPerBatch)) 
    {
        data = el.data;
        newval = data.replace(bw, ReplacementText);
        if (newval.length != data.length ||  newval != data)
        {
            //check the length first since its quicker than a a string comparison.
            //only change the value if we need to. its quicker.
            el.data = newval;
        }
        loopCount++;
    }

    if (el != null)
    {
        //more work left to do
        i--;
        GoNow(MillisecondsPauseBetweenBatches);
    }
    else
    {
        //we're done
        DoneNow();
    }
}

function DoneNow()
{
    var et = new Date().valueOf();
    //alert("Milliseconds to complete: " + (et - st).toString()); //timer code
}

function GoNow(WaitUntil)
{
    window.setTimeout(CleanSome, WaitUntil); 
}

//spin the initial "thread"
GoNow(0);

 })
 ();

我希望用这18位诗人中的一个名字随机替换其中一个发誓的话语,

Walt Whitman
Edna St. Vincent Millay
Emily Dickinson
E. E. Cummings
Robert Frost
Nikki Giovanni 
Ralph Waldo Emerson
T.S. Elliot
Edgar Allan Poe
W.B. Yeats
Lord Tennyson
Maya Angelou
Langston Hughes
Sylvia Plath
Elizabeth Bishop
Anne Sexton
Anne Bradstreet
Gwendolyn Elizabeth Brooks

1 个答案:

答案 0 :(得分:0)

怎么样:

var text = "hello cruel world, hello",
    goodWords = ["yo","man"],
    badWords = ["hello","world"];
badWords.forEach(function(bad){
  var word = goodWords[Math.floor(Math.random()*goodWords.length)],
      reg = new RegExp(bad,"gi");
  text = text.replace(reg,word);
})

Code Pen

怎么样?