stuart.crouch / Humble Auto-redeemer

// ==UserScript==
// @name         Humble Auto-redeemer
// @version      3.20
// @description  Automatically opens any revealed steam keys in a new window. Combine with https://openuserjs.org/scripts/stuart.crouch/Steam_auto-activate to one-click redeem your games
// @author       Stuart Crouch
// @match        *://*.humblebundle.com/downloads?key=*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js
// @grant        none
// @icon         https://humblebundle-a.akamaihd.net/static/hashed/47e474eed38083df699b7dfd8d29d575e3398f1e.ico
// @licence      MIT
// ==/UserScript==


var targets = ['.whitebox',
               '.key-list'];

$(document).ready(function() {
    function addObserverIfDesiredNodeAvailable() {
        var foundTarget;
        var found = targets.some(function(target) {

            var targetNode = document.querySelectorAll(target);
            if (targetNode.length >= 1) {
                //The node we need does not exist yet.
                //Wait 500ms and try again
                foundTarget = target;
                return true;
            }
        });

        if (!found) {
            window.setTimeout(addObserverIfDesiredNodeAvailable,500);
            return;
        }

        // It exists, observe it to see what the user clicks
        observeTarget(foundTarget);
    }
    addObserverIfDesiredNodeAvailable();

    function observeTarget(targetNodeName) {

        console.log ("observing " + targetNodeName);
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (mutation.type == 'childList') {
                    if (mutation.addedNodes.length >= 1) {
                        openSteamLinksInNewWindow(mutation);
                    }
                }
            });
        });

        observer.observe( $(targetNodeName).get(0), {
            attributes: false,
            childList: true,
            subtree: true
        });
    }

});

function openSteamLinksInNewWindow(mutation){
    mutation.addedNodes.forEach(function (currentNode){
        var steamButtons = $(currentNode).find(".steam-redeem-button");
        for (var i = 0; i < steamButtons.length; i++) {
            var win = window.open($(steamButtons[i]).attr('href'), '_blank');
        }
    });
}