Класс Google использует для описаний st
, поэтому вот улучшение решения Dr.Molle:
//get the text
var text=document.querySelector(".st"),output=[];
//loop through the descriptions
for(var i=0;i<text.length;i++){
//find the words
var words=text[i].innerText.match(/\b([a-z]{3,})\b/gi);
//push the array to the output variable
output.push.apply(output,words);
}
//pick a word
var theWord=output[Math.floor(Math.random()*output.length)];
//now theWord has a randomly chosen word
alert(theWord);
И, чтобы выбрать несколько слов:
//change the 10 to the max number of words
var amount=Math.floor(Math.random()*10),theWords="";
//get the text
var text=document.querySelector(".st"),output=[];
//loop through the descriptions
for(var i=0;i<text.length;i++){
//find the words
var words=text[i].innerText.match(/\b([a-z]{3,})\b/gi);
//push the array to the output variable
output.push.apply(output,words);
}
//pick a word
var theNumber=Math.floor(Math.random()*output.length);
//loops the code the number of times randomly chosen
for(var i=0;i<amount;i++){
//add on to the string
theWords+=(i==0?"":" ")+output[theNumber+i];
}
//now theWords has a random number of consecutive words
alert(theWords);
Ad @ м