lyonhaert / spanshR2RMem

// ==UserScript==
// @name         spanshR2RMem
// @namespace    https://openuserjs.org/users/lyonhaert
// @version      1.0.3
// @description  Uses localstorage to remember Completed by planet name for Spansh's Road 2 Riches.
// @author       lyonhaert
// @copyright    2018, lyonhaert (https://openuserjs.org/users/lyonhaert)
// @license      MIT
// @match        https://*.spansh.co.uk/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var objRefs = {};
    var storedToObjsLookup = {};
    var storedStr = window.localStorage.getItem('spanshR2RMem');
    var stored = {};
    var setupIdentifier = 'spanshR2RMem';

    if (storedStr !== null && storedStr.length > 0) {
      stored = JSON.parse(storedStr);
    }

    function saveStored() {
        window.localStorage.setItem('spanshR2RMem', JSON.stringify(stored));
    }

    function handleCheckbox(e) {
        var isChecked = e.target.checked;
        var objName = e.target.value;
        var storedName = objRefs[objName].storedName;
        stored[storedName].completed = isChecked;
        var others = storedToObjsLookup[storedName];
        if (others !== undefined && others !== null && others.length > 0) {
            for (var i = 0; i < others.length; i++) {
                if (others[i] !== objName) {
                    objRefs[others[i]].checkbox.checked = isChecked;
                }
            }
        }
        saveStored();
    }

    function checker() {
        doSetup();
        setTimeout(function () {
            checker();
        }, 250);
    }

    function doSetup() {
        if (window.location.href.toLowerCase().indexOf('/riches/results/') < 0)
            return;

        var rows = $('.table-striped tbody tr');
        if (rows.length === 0)
            return;

        if (rows[0].parentNode.id === setupIdentifier)
            return;

        console.log('Beginning spanshR2RMem setup');
        rows[0].parentNode.id = setupIdentifier;
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            var cb = row.children[0].firstElementChild;
            var name = row.children[2].innerText;

            if (name !== null && name.length > 0) {
                var objName = i.toString() + '_' + name;
                if (stored[name] === undefined) {
                    stored[name] = {completed: false, exported: false};
                }
                objRefs[objName] = { checkbox: cb, storedName: name };
                if (storedToObjsLookup[name] === undefined) {
                    storedToObjsLookup[name] = [];
                }
                storedToObjsLookup[name].push(objName);
                var exportMode = cb.id !== null && cb.id.length > 0;
                if (!exportMode) {
                    cb.value = objName;
                    cb.checked = stored[name].completed;
                    $(cb).change();
                    $(cb).on('change', function (e) {
                        handleCheckbox(e);
                    });
                } else {
                    cb.value = objName; //maybe?
                    if (stored[name].exported === false) {
                        cb.checked = stored[name].completed;
                        $(cb).change();
                        stored[name].exported = true;
                    }
                }
            }

            saveStored();
        }
    }

    checker();
})();