2

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

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/

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!

3 Answers 3

3

See: wiki.greasespot.net/Generate_Click_Events .

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!)

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It only seems to do one "load more" link at a time, is that what the [0].dispatchEvent means?
What are these, Mozilla DOM events? You can just mix those into jQuery?
In part. The [0] was also used to switch back from a jNode to an HTML element -- which is required for dispatchEvent(). ... I don't use Reddit and did not realize that there would be more than one such link (that page currently has 155!!!), or that you'd want to click more than one at once. (But I see that that was in your question. See my updated answer. ... We're not mixing Mozilla events into jQuery, we're just using jQuery to make node selection and manipulation easier. jQuery was designed to make such things easier.
PS. Clicking all those links at once may cause a bottleneck, crash, or throttling by Reddit (or might not; could get lucky). If it does, then please open another question for that issue.
0

You are trying to simulate click event but this only works on events attached with jQuery. Events on load more comments links at reddit attached using html attributes. I.e:

onclick="return morechildren(this, 't3_i7hb5', 'c21ko21,c21kesz,c21klil,c21ko45,c21kro5,c21l90v,c21lo38', 3, '')"

to solve your problem you need to extract values from this attributes and call them separately.

Comments

0

If you have jQuery loaded on the page you could just trigger the click event using jQuery

1 Comment

No, because the click handler was not set with jQuery. "event handlers attached with .bind() or one of its shortcut methods are triggered"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.