Krylik / SharksAutoplay

// ==UserScript==
// @name         SharksAutoplay
// @version      0.1
// @description  For all sharkkind
// @author       Krylik
// @match        http://cirri.al/sharks/
// @grant        none
// ==/UserScript==
/* jshint -W097 */
'use strict';

// Create auto harvest button
var autoHarvestButton = document.createElement('button');
autoHarvestButton.setAttribute('id', 'autoHarvestButton');
autoHarvestButton.setAttribute('class', 'min-block');
autoHarvestButton.innerHTML = "  Auto Harvest (off)  ";
var autoHarvestEnabled = false;
autoHarvestButton.addEventListener('click', function() {
    if (autoHarvestEnabled) {
        autoHarvestEnabled = false;
        autoHarvestButton.innerHTML = "  Auto Harvest (off)  ";
    } else {
        autoHarvestEnabled = true;
        autoHarvestButton.innerHTML = "  Auto Harvest (on)  ";
    }
});

// Create auto buyall button
var autoBuyallButton = document.createElement('button');
autoBuyallButton.setAttribute('id', 'autoBuyallButton');
autoBuyallButton.setAttribute('class', 'min-block');
autoBuyallButton.innerHTML = "  Auto Buy-all (off)  ";
var autoBuyallEnabled = false;
autoBuyallButton.addEventListener('click', function() {
    if (autoBuyallEnabled) {
        autoBuyallEnabled = false;
        autoBuyallButton.innerHTML = "  Auto Buy-all (off)  ";
    } else {
        autoBuyallEnabled = true;
        autoBuyallButton.innerHTML = "  Auto Buy-all (on)  ";
    }
});

function addButtons() {
    var buttonParent = document.querySelector('#helpButton');
    if (buttonParent) {   
        buttonParent.parentElement.appendChild(autoBuyallButton);
        buttonParent.parentElement.appendChild(autoHarvestButton);
    }
    
    // Set up event listener on the menu list with a recursive call
    // to addButtons, because the menu is destroyed and rebuilt when you
    // change tabs.
    document.querySelector('#tabList').addEventListener('click', addButtons);
}

// Call addButtons once for initial page load
addButtons();

// Timer
var autoInterval = setInterval(function() {
    if (autoBuyallEnabled) {
        autoBuyAll();
    } else if (autoHarvestEnabled) {
        autoHarvest();
    }
}, 50);

function autoHarvest() {
    var allButtons = Array.from(document.querySelector('#buttonList').children);
    allButtons.filter(function(b) {
        return b.innerHTML.match(/Cost:/) ? false : true;
    }).forEach(function(b) {
       b.click();
    });
}

function autoBuyAll() {
    var allButtons = Array.from(document.querySelector('#buttonList').children);
    allButtons.forEach(function(b) {
        b.click();
    });
}