11
\$\begingroup\$

I wrote a quick and dirty auto-clicker for the StackEgg. Any comments, both on code and effectiveness?

function doRound() {
    var stats = [];
    $(".egg-stat").each(function () {
        // Find the number of hearts for each stat.
        var title = $(this).attr("title");
        title && stats.push(parseInt(title, 10));
    });

    // Find the lowest stat and click the button next to it.
    var i = stats.indexOf(Math.min.apply(Math, stats));
    var buttons = $(".egg-action").find("button");

    buttons[i].click();
}

window.setInterval(doRound, 7000);
\$\endgroup\$
5
  • \$\begingroup\$ What web browser is this designed for? \$\endgroup\$
    – Phrancis
    Mar 31, 2015 at 22:05
  • \$\begingroup\$ When I try to run this, I am presented with an error: Cannot read property 'click' of undefined. I am using Google Chrome. What browser are you using? \$\endgroup\$
    – SirPython
    Mar 31, 2015 at 22:06
  • \$\begingroup\$ @SirPython You need to have Egg window open \$\endgroup\$
    – Jonathan
    Mar 31, 2015 at 22:09
  • 2
    \$\begingroup\$ @Malachi tested in IE and Chrome but should work everywhere. It doesn't only click the close button. \$\endgroup\$
    – Jonathan
    Mar 31, 2015 at 22:13
  • \$\begingroup\$ @Jonathan, sorry I did realize that, but I mentioned it in Chat and not here. \$\endgroup\$
    – Malachi
    Apr 1, 2015 at 1:55

2 Answers 2

2
\$\begingroup\$
  • Best voting decision should be made just before the 20s timer ends to count most of the votes.
  • Instead of $(".egg-action").find("button") use a '.egg-action > button" selector
  • Too much jQuery, go back to VanillaJS and for loops

PS: works on Firefox also

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why would you vote to boost the highest stat? The lowest stat is the one that needs it the most. If you just max out one stat and keep clicking its button, your actions will do no good and your site will die. \$\endgroup\$ Apr 1, 2015 at 0:32
  • \$\begingroup\$ @user2357112 edited. left only suggestions, thanx \$\endgroup\$
    – CSᵠ
    Apr 1, 2015 at 15:33
0
\$\begingroup\$

Some improvements: drop jQuery and checks for red hearts. This should be able to solo win an internet.

Add check for red hearts. parseInt(title, 10) will return NaN.

// Check for red hearts.
isNaN(stats[stats.length-1]) && (stats[stats.length-1] = -1);

Vanilla has better performance:

document.querySelectorAll(".egg-stat").forEach(function (elem) {...});
var buttons = document.querySelectorAll(".egg-action button");
var title = elem.title;

Add this to use forEach with NodeList:

NodeList.prototype.forEach = Array.prototype.forEach;

JS

NodeList.prototype.forEach = Array.prototype.forEach;

function doRound() {
    var stats = [];
    document.querySelectorAll(".egg-stat").forEach(function (elem) {
        // Find the number of hearts for each stat.
        var title = elem.title;
        title && stats.push(parseInt(title, 10));
        // Check for red hearts.
        isNaN(stats[stats.length-1]) && (stats[stats.length-1] = -1);
    });

    // Find the lowest stat and click the button next to it.
    var i = stats.indexOf(Math.min.apply(Math, stats));
    var buttons = document.querySelectorAll(".egg-action button");

    buttons[i].click();
}

window.setInterval(doRound, 7000);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.