单击链接的Greasemonkey Jquery脚本 [英] Greasemonkey Jquery Script to Click Links

查看:88
本文介绍了单击链接的Greasemonkey Jquery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做我的第一个润滑脂脚本.我对jquery和javascript相当陌生,所以对我来说要容易一些.

I'm trying to do my first greasemonkey script. I'm fairly new to jquery and javascript, so be easy on me.

这是我到目前为止所拥有的.

Here is what I have so far.

// ==UserScript==
// @name           load all page comments
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// @namespace      none
// @include        http://www.reddit.com/*
// ==/UserScript==

setInterval( function () {
  window.alert("Hello World!");
  $("a:contains('load more comments')").click();
}, 10000);

这里的目标是像这样在示例reddit页面上单击所有加载更多评论"页面,并每10秒循环执行一次.

The goal here is to click on all of the "load more comments" page on a sample reddit page like this, and to loop doing it every ten seconds.

http://www.reddit.com/r/AskReddit/comments /i7hb5/why_assign_gender_to_public_bathrooms_if_there_is/

现在,仅会弹出"hello world"警报,但未单击链接.因此,间隔功能正常工作,但无法加载更多注释.不知道从这里去哪里. 加载更多评论"字符串中的空格会破坏它吗?

Right now, only the hello world alert pops up, but the links aren't clicked. So the interval function is working, but loading more comments isn't. Not sure where to go from here. Could the spaces in the 'load more comments' string be breaking it?

任何帮助,我们将不胜感激.谢谢!

Any help greatly appreciated. Thanks!

推荐答案

请参阅: wiki.greasespot.net/Generate_Click_Events .

Reddit链接会触发JavaScript,而不触发由jQuery设置的JS.

That Reddit link fires JavaScript and not JS that was set with jQuery.

这意味着在这种情况下,您需要发送一个实际的鼠标事件,如下所示:

Which means that in this case, you need to send an actual mouse event, like so:

setInterval ( function () {

    var clickEvent  = document.createEvent ("HTMLEvents");
    clickEvent.initEvent ("click", true, true);

    $("a:contains('load more comments')")[0].dispatchEvent (clickEvent);
}, 10000);


糟糕!我没有看到该问题提到单击加载更多评论"中的"全部". (并且该页面有数百个!)


Oops! I did not see that the question mentioned clicking "all of the 'load more comments'". (And that page has hundreds of them!)

为此,请使用jQuery的each()函数...

To do that, use jQuery's each() function...

setInterval ( function () {

    var moreLinks       = $("a:contains('load more comments')");

    moreLinks.each ( function () {

        var clickEvent  = document.createEvent ("HTMLEvents");
        clickEvent.initEvent ("click", true, true);
        this.dispatchEvent (clickEvent);
    } );
}, 10000);

这篇关于单击链接的Greasemonkey Jquery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆