これは、説明から任意の数のランダムな単語を取得し、さまざまなGoogle検索ページを少し適切に処理する完全なGreasemonkeyスクリプトです(ページの取得方法に応じて、DOMがわずかに変更されます)。
// ==UserScript==
// @name _RandomWord from results
// @include http://www.google.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==
function GrabRandomWords () {
/*--- Google search results are wrapped in a list with the id "rso".
Here, we want just the descriptions in the results, so we know
that title and link tag tags can be excluded.
(Alas, Google's search results pages vary quite a bit in the
detailed structure of their HTML.)
*/
var descriptText = $("#rso li *:not(a,h1,h2,h3)").text ();
//--- Split into words. Change the "{1,}", if short words like "a" are to be excluded.
var words = descriptText.match (/\b(\w{1,})\b/g);
//--- Pick 5 random words and store them in an array.
if (words) {
var ourRandWords = [];
for (var J = 0; J < 5; ++J)
ourRandWords.push ( words[ Math.floor (words.length * Math.random() ) ] );
alert (ourRandWords);
}
}
//--- We must wait until page has fully loaded, because the results are usually Ajaxed in.
window.addEventListener ("load",
function () {
/*--- Page is "loaded", but results are still not in, still need a short delay.
Note that listening for DOMSubtreeModified doesn't seem to work well.
*/
setTimeout (function() { GrabRandomWords (); }, 666);
},
false
);