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

发布于 2024-11-24 01:17:59 字数 312 浏览 1 评论 0原文

我正在做一个小项目,但我似乎陷入了困境。希望你们中的一些伟大的人能够在这方面帮助我。

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

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

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

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

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

I am working on a little project but I seem to be stuck at this point. Hopefully, some of you great people will be able to help me out on this.

I am trying to figure out a simple and efficient way of pick out a RANDOM word or words from a page of search engine results. This is the part I am stuck on.

After picking it out, I would store the word(s) in a variable.

The search results look like this: http://i54.tinypic.com/34fllw1.png

Thanks in advance. Any tips/help would be greatly appreciated!

EDIT: Is there a way I would be able to pick out a string of consecutive words of random length?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹倦 2024-12-01 01:18:15

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@m

The class Google uses for descriptions is st, so here's an improvement of Dr.Molle's solutiom:

//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);

And to pick a few words out:

//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@m

最后的乘客 2024-12-01 01:18:15

这是一个完整的 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
);

Here's a complete Greasemonkey script that grabs an arbitrary number of random words, just from the descriptions, and handles the variety of Google search pages a little better (They change the DOM slightly, depending on how the page was obtained.)

// ==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
);
洋洋洒洒 2024-12-01 01:18:13

下面是一个适用于 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 的字符串

Here is an example that works with 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())]);

The searchresults are listed in an element with the ID "rso".
The regexp matches strings consisting of at least 3 chars a-z

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文