miketrethewey / ALttPR Generation Randomizer

// ==UserScript==
// @namespace	https://openuserjs.org/users/miketrethewey
// @name		ALttPR Generation Randomizer
// @description	Randomizes the form options in SM/ALttPR
// @copyright	2018, miketrethewey (https://openuserjs.org/users/miketrethewey)
// @license		MIT
// @version		1.0.0
// @include		https://alttpr.com/*
// @include     https://alttsm.com/*
// @include     https://samus.link/*
// @include     https://alttsm.speedga.me/*
// @include		https://smalttp.speedga.me/*
// @grant		none
// ==/UserScript==

// ==OpenUserJS==
// @author	miketrethewey
// ==/OpenUserJS==

$(window).on('load', function() {
    // Get VT Version
    let vt = document.getElementsByClassName("input-group-prepend").length > 0 ? 30 : 29;

    // Make DOM handles
    let selectables, toggles = null;

    // Make function to attach to button
    function randomizeSettings() {
        // Get select boxes
        if(vt == 30) {
            selectables = document.getElementsByClassName("input-group-prepend");
        } else if(vt == 29) {
            selectables = document.querySelectorAll(".row .input-group-addon");
        }
        // Cycle through select boxes
        for(let selectable in selectables) {
            if(selectables.hasOwnProperty(selectable)) {
                selectable = selectables[selectable];
                // Get select box label
                let catLabel = selectable.parentNode.children[1].innerText;
                // Get options for this select box
                let options = null;
                if(vt == 30) {
                    options = selectable.parentNode.querySelectorAll(".multiselect__element span");
                } else if(vt == 29) {
                    options = selectable.parentNode.getElementsByTagName("li");
                }
                if(options.length > 0) {
                    // Choose a random option
                    let randomOption = options[Math.floor(Math.random() * options.length)];
                    // Set the random option
                    if(vt == 30) {
                        randomOption.click();
                        randomOption.blur();
                    } else if(vt == 29) {
                        randomOption.getElementsByClassName("glyphicon")[0].click();
                    }
                }
            }
            window.focus();
            if(document.activeElement) {
                document.activeElement.blur();
            }
        }

        // Get toggle switches
        toggles = document.querySelectorAll(".vue-js-switch");
        // Cycle through toggle switches
        for(let toggle in toggles) {
            if(toggles.hasOwnProperty(toggle)) {
                toggle = toggles[toggle];
                // Get toggle switch label
                let catLabel = toggle.parentNode.children[1].innerText;
                if(Math.random() < 0.5) {
                    // 50% chance to change
                    toggle.click();
                }
            }
        }
    }

    // Make button to attach to menu
    let randomizeButton = document.createElement("button");
    randomizeButton.setAttribute("role","button");
    randomizeButton.innerText = "Randomize Settings!";
    randomizeButton.onclick = randomizeSettings;

    // Prepare to get header element
    let header = null;

    if(vt == 30) {
        // Style button
        randomizeButton.className = "btn btn-light border-secondary float-right";
        // Get header
        header = document.getElementsByClassName("card-header bg-success card-heading-btn")[0];
    } else if(vt == 29) {
        // Style button
        randomizeButton.className = "btn btn-success pull-right";
        // Get header
        header = document.getElementsByClassName("panel-heading-btn")[0];
    }

    // Add button to header
    if(header) {
        if(vt == 30) {
            randomizeButton.style.margin = "0 20px 0 0";
            header.appendChild(randomizeButton);
        } else if(vt == 29) {
            header.insertBefore(randomizeButton, header.firstChild);
        }
    } else {
        console.log("Something happened");
    }
});