Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Greasemonkey-从搜索引擎结果页面中随机选取单词_Javascript_Greasemonkey_Userscripts - Fatal编程技术网

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

Javascript Greasemonkey-从搜索引擎结果页面中随机选取单词,javascript,greasemonkey,userscripts,Javascript,Greasemonkey,Userscripts,我正在做一个小项目,但我似乎被困在这一点上。希望你们中的一些伟大的人能够在这方面帮助我 我正试图找出一种简单有效的方法,从搜索引擎结果页面中随机挑选一个或多个单词。这是我一直坚持的部分 在选择它之后,我会将单词存储在一个变量中 搜索结果如下所示: 提前谢谢。任何提示/帮助都将不胜感激 编辑:有没有一种方法可以让我挑出一个随机长度的连续单词串?这里有一个谷歌网站的例子 //get the text var text=document.getElementById('rso').textConten

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

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

在选择它之后,我会将单词存储在一个变量中

搜索结果如下所示:

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


编辑:有没有一种方法可以让我挑出一个随机长度的连续单词串?

这里有一个谷歌网站的例子

//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”的元素中。

regexp匹配由至少3个字符组成的字符串a-z

Google用于描述的类是
st
,因此这里是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);
//获取文本
var text=document.querySelector(“.st”),输出=[];
//循环浏览描述

对于(var i=0;i这里有一个完整的Greasemonkey脚本,它仅从描述中获取任意数量的随机单词,并更好地处理各种Google搜索页面(它们稍微改变DOM,具体取决于页面的获取方式)

/==UserScript==
//@name\u来自结果的随机词
//@包括http://www.google.com/*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
//==/UserScript==
函数词(){
/*---谷歌搜索结果包装在一个id为“rso”的列表中。
在这里,我们只需要结果中的描述,所以我们知道
可以排除标题和链接标记。
(唉,谷歌的搜索结果页面在未来几年变化很大
他们的HTML的详细结构。)
*/
var descriptText=$(“#rso li*:not(a,h1,h2,h3)”)。text();
//---拆分为单词。如果要排除像“a”这样的短单词,请更改“{1,}”。
var words=descriptText.match(/\b(\w{1,})\b/g);
//---选择5个随机单词并将它们存储在一个数组中。
如有(字){
var ourRandWords=[];
对于(var J=0;J<5;++J)
ourRandWords.push(单词[Math.floor(words.length*Math.random())];
警惕(我们的语言);
}
}
//---我们必须等到页面完全加载,因为结果通常是半轴加载的。
window.addEventListener(“加载”,
函数(){
/*---页面已“加载”,但结果仍不在,仍需要短暂延迟。
请注意,侦听DOMSubtreeModified似乎效果不佳。
*/
setTimeout(函数(){GrabRandomWords();},666);
},
假的
);

您只希望单词来自可见页面,而不是html代码?是的,最好是来自结果中的描述。谢谢您的帮助!我明白了,这确实将选择范围缩小到了我想要的范围。谢谢您的建议!我还想问,有没有办法挑选出一系列的co随机长度的执行字?尝试用
[a-z]{10,}
替换
[a-zA-z]{3,}
(在a-z后面加空格)。我还没有测试过这个,所以我不确定它是否有效。Ad@mThat太棒了!还有一件事,有没有办法排除某些单词被选中?例如,页面上有很多重复的单词,例如“赞助商”,我不想将其包含在我的选择中。再次感谢!使用我刚才在答案中输入的代码,并将其放置在
thwWords+=
行上方:
if(输出[theNumber+I].toLowerCase()==“赞助商”){继续;}
。在JavaScript中继续意味着继续循环的下一次迭代。Ad@mWow~z~这太好了。谢谢你的补充意见!
//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);
// ==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
);