ziep / VACalculator

// ==UserScript==
// @name        VACalculator
// @author   ziep
// @version  2.1.1
// @icon      https://resources.patchworkklub.sk/rapp/icon.png
// @description add a mini version of the Verbal abuse calculator as a dialog box
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @require  https://resources.patchworkklub.sk/js/jquery-2.2.4.min.js
// @require  https://raw.githubusercontent.com/mckamey/countdownjs/master/countdown.min.js
// @require  https://raw.githubusercontent.com/jeresig/jquery.hotkeys/master/jquery.hotkeys.js
// @require  https://resources.patchworkklub.sk/js/jquery-cookie.js
// @require  https://raw.githubusercontent.com/bunkat/later/master/later.min.js
// @require  https://raw.githubusercontent.com/jeresig/jquery.hotkeys/master/jquery.hotkeys.js
// @require  https://rap.heroesofnewerth.com/js/jquery.dataTables.min.js
// @require  https://rap.heroesofnewerth.com/js/dataTables.bootstrap.min.js
// @require  https://rap.heroesofnewerth.com/js/moment.js
// @require  https://code.jquery.com/ui/1.12.1/jquery-ui.js
// @grant       GM_addStyle
// @match     *://rap.heroesofnewerth.com/chat.php*
// @updateURL https://openuserjs.org/meta/ziep/VACalculator.meta.js
// @downloadURL https://openuserjs.org/install/ziep/VACalculator.user.js
// @copyright 2020, ziep (https://openuserjs.org/users/ziep)
// @license 0BSD

// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
var $data = ['hope you die', 'kill ur self', 'kill urself', 'kill yourself', 'kys', 'beaner', 'bög', 'chink', 'coon', 'dot', 'dothead', 'dune coon', 'fag', 'faggit', 'fag', 'fascina', 'fgt', 'gandon', 'gook', 'ibne', 'indio', 'macaco', 'marica', 'maricon', 'negro', 'nggr', 'nigga', 'peder', 'pidar', 'pidor', 'nigger', 'spic', "wetback", "wet back", "untermensch", "nibba"];
var $extra = ['cancer', "aids", "corona", "ebola"];
var $VACalcDialog;
function addHighlight() {
  $("head").append('<style type="text/css">' +
    '.t1Word{color:#ff0000;}' +
    '.extra_words{color: #FFA500;}')

  $(".chatAdd").each(function () {

    var new_elem = '';
    for (var i = 0; i < $data.length; i++) {

      if ($(this).text().toLowerCase().includes($data[i])) {
        if (new_elem.length > 0) {
          new_elem = new_elem.replaceAll($data[i], ('<span class="t1Word">' + $data[i] + '</span>'));
        }
        else {
          new_elem = '<span class="chatAdd" style="color: rgb(255,255,255);">' + $(this).text().toLowerCase().replaceAll($data[i], ('<span class="t1Word">' + $data[i] + '</span>') + '</span>');
        }
      }
      else if (i < $extra.length && $(this).text().toLowerCase().includes($extra[i])) {
        if (new_elem.length > 0) {
          new_elem = new_elem.replaceAll($extra[i], ('<span class="extra_words">' + $extra[i] + '</span>'));
        }
        else {
          new_elem = '<span class="chatAdd" style="color: rgb(255,255,255);">' + $(this).text().toLowerCase().replaceAll($extra[i], ('<span class="extra_words">' + $extra[i] + '</span>') + '</span>');
        }
      }
      else if (new_elem.length) {
        continue;
      }

    }
    if (new_elem.length) {
      $(this).replaceWith(new_elem);
    }
  });
}
(function () {
  $('head').append('<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">'+ //dunno if Smilie grief me but I need jquery 1.12.1 xD
    "<style type='text/css'>" +
    ".guilty{color:red;display:inline-block;font-size:1.5rem;}" +
    ".warning{color:#ff9000;display:inline-block;font-size:1.5rem;}" +
    ".innocent{color:green;display:inline-block;font-size:1.5rem;}" +
    ".result{font-size:1.5rem;}" +
    ".t1Word{color:#ff0000;}" +
    "input[type='number'] {-webkit-appearance: textfield;-moz-appearance: textfield;appearance: textfield;}input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none;}" +
    //                    "@import 'https://fonts.googleapis.com/css?family=Lato:400,400italic,700|Sansita+One;max-width:100%;margin:25px;font-size:calc(1rem + 0.25vh);overflow:auto"+
    "</style>");
  $("body").append('                                                          \
    <div id="gmPopupContainer" title = "VACalculator" scrolling = "yes" class="gmPopupContainer"> \
    <form><div priorsButton><p style="display: inline-block;">Priors within the last 9 months?</p>\
    <label for="yes" style="cursor: pointer;">\
    <input type="radio" style="display: inline-block;cursor: pointer;" id="yes" name="priors" value="Yes">Yes</label>\
    <label for="no" style="cursor: pointer;">\
    <input type="radio" style="display: inline-block;cursor:pointer;" id="no" name="priors" value="No" checked>No</label>\
    <br></div></form>\
    <body><div calc><input style="cursor: pointer;" type="Button" id="resetButton" value="Reset" title="Set all fields to 0">\
    <div inputBoxes style="width: 70%";>\
    <div>How many T1: <input class="tier" id="t1" type="number" step="1" min="0" required value="0" /><input style="cursor: pointer;" type="Button" class="add" value="+1">\
    &nbsp&nbsp<input style="cursor: pointer;" class="sub" type="Button" value="-1"></div>\
    <div><br><br><br>How many T2: <input class="tier" id="t2" type="number" step="1" min="0" required value="0" />\
    <input style="cursor: pointer;" type="Button" class="add" value="+1">&nbsp&nbsp<input style="cursor: pointer;" type="Button" class="sub" value="-1"></div>\
    <div><br><br><br>How many T3: <input class="tier" id="t3" type="number" step="1" min="0" required value="0" />\
    <input style="cursor: pointer;" type="Button" class="add" value="+1" >&nbsp&nbsp<input style="cursor: pointer;" type="Button" class="sub" value="-1"></div>\
    <div><br><br><br>How many T4: <input class="tier" id="t4" type="number" step="1" min="0" required value="0" />\
    <input style="cursor: pointer;" type="Button" class="add" value="+1" >&nbsp&nbsp<input style="cursor: pointer;" type="Button" class="sub" value="-1" ></div>\
    </div><br><br></div> \
    </head></body></html>\
    <input style="cursor: pointer;" type="Button" id="calcButton" value="Calculate"><br>\
    </div> \
    ');

  $(function () {
    $VACalcDialog = $("#gmPopupContainer").dialog({
      autoOpen: false,
      maxWidth: 1000,
      maxHeight: 1000,
      height: 760,
      width: 700,
    });
  });

  $(function () {
    window.addEventListener("load", () => {
      addButton("ziep's VA calculator");
      addHighlight();

    });

    function addButton(text, onclick, cssObj) {
      cssObj = cssObj || {
        position: "fixed",
        top: "680px",
        right: "200px",
        "z-index": "999",
        fontWeight: "600",
        fontSize: "14px",
        cursor: "pointer",
        //backgroundColor: "#00cccc",
        color: "black",
        border: "none",
        padding: "10px 20px"
      };
      let button = document.createElement("button"),
        btnStyle = button.style;
      document.body.appendChild(button);
      button.innerHTML = text;
      // Settin function for button when it is clicked.
      button.onclick = selectReadFn;
      Object.keys(cssObj).forEach((key) => (btnStyle[key] = cssObj[key]));
      return button;
    }

    function selectReadFn() {
      $VACalcDialog.dialog("open");
    }
  });

  $("#calcButton").click(function () {
    $('.result').remove();
    //<!-- variable declarations-->
    var $t1 = parseFloat($("#t1").val());
    var $t2 = parseFloat($("#t2").val());
    var $t3 = parseFloat($("#t3").val());
    var $t4 = parseFloat($("#t4").val());
    //<!--$op = document.getElementById("opr").value;-->
    var $t1WarningRatio = 7;
    var $t2WarningRatio = 2.33334;
    var $t3WarningRatio = 1.4;

    var $t1GuiltyRatio = 4.5;
    var $t2GuiltyRatio = 1.8;
    var $t3GuiltyRatio = 1.286;
    var $priors = $('#yes').is(':checked');
    var $hasPriors = '';
    var $need = '9';
    var $totalLinesWarning = (($t1 * $t1WarningRatio) + ($t2 * $t2WarningRatio) + ($t3 * $t3WarningRatio) + $t4).toFixed(2);
    var $totalLinesGuilty = (($t1 * $t1GuiltyRatio) + ($t2 * $t2GuiltyRatio) + ($t3 * $t3GuiltyRatio) + $t4).toFixed(2);

    var $guilty = ($totalLinesGuilty >= 9);
    var $warning = ($totalLinesWarning >= 7);
    if ($priors) {
      $guilty = ($guilty || $warning);
      $totalLinesGuilty = $totalLinesWarning;
      $hasPriors = ' with priors';
      $need = '7';
    }
    if ($guilty) {
      $(this).parent().append('<div class="result"><b>VERDICT</b>: <p class="guilty">Guilty!</p></p>');
      $(this).parent().append('<div class="result">&#9989; Sufficient for <p class="guilty">Guilty' + $hasPriors + '! </p> (<p class ="guilty">Guilty</p> score of ' +
        $totalLinesGuilty + '/' + $need + ')</div>'
      );
    }
    else if ($warning) {
      $(this).parent().append('<div class="result"><b>VERDICT</b>: <p class="warning">Warning!</p></p>');
      $(this).parent().append('<div class="result">&#10060; Insufficient for <p class="guilty">Guilty! </p> (<p class ="guilty">Guilty</p> score of ' +
        $totalLinesGuilty + '/' + $need + ')</div>'
      );
      $(this).parent().append('<div class="result">&#9989; Sufficient for <p class="warning">Warning! </p> (<p class ="warning">Warning</p> score of ' +
        $totalLinesWarning + '/7' + ')</div>'
      );
    }
    else {
      $(this).parent().append('<div class="result"><b>VERDICT</b>: <p class="innocent">Innocent!</p></p>');
      $(this).parent().append('<div class="result">&#10060; Insufficient for <p class="guilty">Guilty! </p> (<p class ="guilty">Guilty</p> score of ' +
        $totalLinesGuilty + '/' + $need + ')</div>');
      $(this).parent().append('<div class="result">&#10060; Insufficient for <p class="warning">Warning! </p> (<p class ="warning">Warning</p> score of ' +
        $totalLinesWarning + '/7' + ')</div>'
      );
    }
  });
  $("#resetButton").click(function () {
    $(".tier").val('0');
    $(".result").remove();
    $("#no").prop("checked", true);
  });
  $('.tier').on('change', function (e) {
    if (!((parseFloat($(this).val()) >= 0) && (parseFloat($(this).val()) <= 100))) {
      $(this).val('0')
    }
  })
  $('.add').click(function () {
    var $counter = parseFloat($(this).parent().children(".tier").val()) + 1;
    $(this).parent().children(".tier").val($counter);
  });
  $('.sub').click(function () {
    var $counter = parseFloat($(this).parent().children(".tier").val()) - 1;
    if (($counter) >= 0) {
      $(this).parent().children(".tier").val($counter);
    }
  });
})();