I’m trying to do my first greasemoney script. I’m fairly new to jquery and javascript, so be easy on me.
Here is what I have so far.
setTimeout(function(){
$('a').each(function(i){
if(this.href && this.innerHTML.indexOf('load more comments') > -1){
toggle(this);
}
});
}, 4000);
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 four seconds.
http://www.reddit.com/r/AskReddit/comments/i7hb5/why_assign_gender_to_public_bathrooms_if_there_is/
Right now, nothing happens at all. I’m not sure how to troubleshoot. Is the script not being launched at all? Is indexOf the right syntax for clicking the links?
Is there an online guide that would walk me though writing a basic greasemoney / jquery script like this?
Any help greatly appreciated. Thanks!
Edit:
Based on Tomalak’s response,
// ==UserScript==
// @name load all page comments
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @namespace none
// @include http://www.reddit.com/*
// ==/UserScript==
setInterval( function () {
$('a:contains(load more comments)').click();
}, 4000);
It’s much easier than you think:
Note that I use
setIntervalinstead ofsetTimeout.jQuery works in such a way that
clickis called on every matched element, i.e. there’s no need foreach()in this situation. Also see the docs on the:containsselector.