bejonwe / PaySafeCard MagicMemory Bot

// ==UserScript==
// @name         PaySafeCard MagicMemory Bot
// @namespace    https://paysafecard.com/MagicMemory
// @version      0.1
// @description  A bot which solves the PaySafeCard MagicMemory. Disclaimer: Using this Bot for a real submission is not allowed!
// @author       Jonathan Weber
// @match        https://paysafecardgames.com/Content/Games/memory/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    if (theMemoryGame !== undefined)
    {
        var cards = {};
        for (var i = 0; i < theMemoryGame.cards.length;i++)
        {
            var split = theMemoryGame.cards[i].img.split('-');
            cards[theMemoryGame.cards[i].id] = split[split.length-1].replace('.png','');
        }
        var cardElements = [];
        var onClick = function() {
            $('.mg__tile--inner').each(function() {
                cardElements.push({
                   el: $(this),
                   id: $(this).attr('data-id')
                });
                var id = $(this).attr('data-id');
                $(this).parent().append('<span>' + cards[id] + '</span>');
            });
            var processElement = function() {
                var tmp = cardElements[0].id;
                cardElements[0].el.click();
                cardElements.splice(0,1);
                for (var s = 0; s < cardElements.length; s++)
                {
                    if (cardElements[s].id == tmp)
                    {
                        cardElements[s].el.click();
                        cardElements.splice(s,1);
                    }
                }
                setTimeout(processElement,1500);
            };
            processElement();
        };
        var bindEvent = function() {
            $('#mg__button--start').unbind('click.helper');
            $('#mg__button--start').on('click.helper',onClick);
            setTimeout(bindEvent,500);
        };
        bindEvent();
    }
})();