Ssieth / FunTriva Name Users

// ==UserScript==
// @name         FunTriva Name Users
// @namespace    http://funtrivia.com/nameusers
// @version      0.3
// @description  Name users in Bill's Music Quiz
// @author       Gaxx
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @match        http://www.funtrivia.com/private/main.cfm?tid=86650
// @match        https://www.funtrivia.com/private/main.cfm?tid=86650
// @grant       GM_getValue
// @grant       GM_addStyle
// @grant       GM_setValue
// @grant        GM_xmlhttpRequest
// @connect      cabbit.org.uk
// @license     MIT
// ==/UserScript==

(() => {
  'use strict';

  var users = {
      "Midian": "Carl",
      "Norfolk-in-Hope": "Sean",
      "will1959": "Bill",
      "SpiderGod": "Jari",
      "phil": "Phil",
      "gaxx": "Gareth",
    "UnderGOverG": "Ady",
      "Wolfe": "Ryan",
      "parbom": "Par",
      "k3lv5y": "Kelvin",
      "Dorset": "Dorset",
      "womble": "Andy B",
    "HundoPWillWin": "Will",
    "flyingdutchman": "Ted",
    "Likablemouse": "Caroline",
    "Whitwell": "Lidsay",
    "kerry": "Kerry",
    "steveaj": "Steve Ambrose-Jones",
    "DJB": "Ted's Wife",
    "thunderpants": "Rob Patterson",
    "Scurfers": "Paula",
      "PBK": "Paul"
  }

  addStyles();
  cabbitLoadSettings(function(data) {
      users=data;
      nameUsers();
  });

    function cabbitLoadSettings(callback) {
        var strData = '';
        var strURL = "https://cabbit.org.uk/eli/?site=eli&user=musicquiznames&hash=musicquiznames";
        GM_xmlhttpRequest({
            method: "GET",
            url: strURL,
            headers: {
                "Content-Type": "application/json"
            },
            onload: function (response) {
                var data = JSON.parse(response.responseText);
                var settingsData = {};
                if (data.status == 'ok') {
                    settingsData = JSON.parse(data.settings);
                }
                if (callback) {
                    callback(settingsData);
                }
            }
        });
    }

    function cabbitSaveSettings(callback) {
        var strData = JSON.stringify(users);
        var strURL = "https://cabbit.org.uk/eli/?site=eli&user=musicquiznames&hash=musicquiznames";
        GM_xmlhttpRequest({
            method: "POST",
            url: strURL,
            data: strData,
            headers: {
                "Content-Type": "application/json"
            },
            onload: function (response) {
                if (callback) {
                    var data = JSON.parse(response.responseText);
                    callback(data);
                }
            }
        });
    }

    function addStyles() {
        var strCSSPointer = ".pointer {cursor: pointer !important; }";
        GM_addStyle(strCSSPointer);
    }

    function loadUsers() {
        users = GM_getValue("users", users);
    }

    function saveUsers() {
        //GM_setValue("users",users);
        cabbitSaveSettings();
    }

    function getUsername($row) {
        console.log($row);
        var userID = $row.find("td:eq(0) a").text();
        var newName = prompt("Please enter the name for " + userID, $row.find("span.username").text());
        if (newName === null) {
            // if cancelled, then exit
            return;
        }
        if (newName) {
            // New name added
            users[userID] = newName;
            saveUsers();
            nameUsers();
        } else {
            // User has set this to be blank soooo... delete from list of peoples
            delete users[userID];
        }
    }

  function nameUsers() {
      var $t = $("body>table.lighttable table.darktable");
      var $trows = $t.find("tr").not(".menubar");
      $(".username-wrapper").remove();
      $trows.each(function () {
          var $row = $(this);
          var name = $row.find("td:eq(0) a").text();
          var $name;
          if (users[name]) {
              $name = $("<span class='username pointer'>" + users[name] + "</span>");
          } else {
              $name = $("<span class='username pointer'>?</span>");
          }
          $name.click( () => {
              getUsername($(this));
          });
          var $namePlus = $("<span class='username-wrapper'> (</span>").append($name);
          $namePlus.append(")");
          $row.find("td:eq(0)").append($namePlus);
      });
  }
})();