繁体   English   中英

Greasemonkey- 从搜索引擎结果页面中挑选随机词

[英]Greasemonkey- Picking random word(s) out of search engine results page

我正在做一个小项目,但我似乎被困在这一点上。 希望你们中的一些伟大的人能够帮助我解决这个问题。

我试图找出一种简单有效的方法来从搜索引擎结果页面中挑选出一个或多个随机单词。 这是我坚持的部分。

挑选出来后,我会将单词存储在一个变量中。

搜索结果如下所示: http://i54.tinypic.com/34fllw1.png

提前致谢。 任何提示/帮助将不胜感激!

编辑:有没有办法可以挑选出一串随机长度的连续单词?

这是一个适用于 google.com 的示例

//get the text
var text=document.getElementById('rso').textContent;
  //find the words 
var words=text.match(/\b([a-z]{3,})\b/gi);
  //pick a word
alert(words[Math.floor(words.length*Math.random())]);

搜索结果列在 ID 为“rso”的元素中。
正则表达式匹配由至少 3 个字符组成的字符串 az

谷歌用于描述的 class 是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);

广告@m

这是一个完整的 Greasemonkey 脚本,它仅从描述中抓取任意数量的随机词,并更好地处理各种 Google 搜索页面(它们会稍微改变 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
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM