zKill page kill sum

mail@pastecode.io avatar
unknown
javascript
2 years ago
4.0 kB
5
Indexable
Never
// ==UserScript==
// @name         zKill page kill sum
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Sums up your kills & losses
// @author       Skipp Doe
// @match        https://zkillboard.com/character/*
// @match        https://zkillboard.com/corporation/*
// @match        https://zkillboard.com/alliance/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict'

    let isMouseDown = false;
    let lastMouseDownElem

    // add styling
    $('head').append('<style type="text/css">.zkill-total-sum-info{display:flex}.uncheck-all{margin:0 8px 8px 20px}.check-all{margin-right:8px}.check-all,.uncheck-all{font-size:14px;cursor:pointer}.zkill-page-kill-sum{margin-left:8px;font-size:14px;font-weight:700;color:#b9b9b9}.total-kills{color:green}.total-losses{color:#a90707}.km-chbox{cursor:pointer;position: absolute;left:-30px;transform: scale(2);}.ad-xl-block{display:none;}</style>')

    // add checkboxes to kills
    $('.killListRow.winwin td:first-child').prepend('<input type="checkbox" class="km-chbox" checked="true">')

    // disable zKill row click propagation because who uses click listeners to propagate URLs in 2021?!?! (ノಠ益ಠ)ノ彡┻━┻
    $('.km-chbox').on('click', function(e){
        e.stopPropagation()
        calculateKms()
    })

    // handle check/uncheck all
    $('.container').on('click', '.check-all', function(){
        $('.km-chbox').prop('checked', true)
        calculateKms()
    })

    $('.container').on('click', '.uncheck-all', function(){
        $('.km-chbox').prop('checked', false)
        calculateKms()
    })

    //Dragging support
    $(".km-chbox").mousedown(function() {
        isMouseDown = true;
        lastMouseDownElem = $(this);
        $(this).prop('checked', !$(this).is(':checked'));
    });

    $(document).mouseup(function() {
        isMouseDown = false;
        calculateKms()
    });

    $(".km-chbox").mouseenter(function() {
        if (isMouseDown) {
            $(this).prop('checked', !$(this).is(':checked'));
        }
    });

    function calculateKms(){
        const killValues = []
        const lossValues = []

        // get all the ISK values from kills
        $('.km-chbox:checked').closest('td').find('a').each(function(){
            killValues.push(parseKill($(this).text()))
        })

        // get all the ISK values from losses
        $('.killListRow.error td:first-child a').each(function(){
            lossValues.push(parseKill($(this).text()))
        })

        // sum it up
        const killSum = `${(sum(killValues) / 1000000000).toFixed(2)}b ISK (${killValues.length})`
        const lossSum = `${(sum(lossValues) / 1000000000).toFixed(2)}b ISK (${lossValues.length})`

        // update existing placeholders if they're already there
        if($('.zkill-page-kill-sum').length){
            $('.total-kills').text(killSum)
            $('.total-losses').text(lossSum)
        }
        // spank them in if they're not
        else{
            $('#killmailstobdy').closest('table').before(`<div class="zkill-total-sum-info"><span class="uncheck-all">☐ Uncheck all</span><span class="check-all">☑ Check all</span>
<span class="zkill-page-kill-sum">Destroyed: <span class="total-kills">${killSum}</span> | Lost: <span class="total-losses">${lossSum}</span></span></div>`)
        }
    }

    // checks the strings provided and multiplies the values accordingly
    function parseKill(killValue){
        const multiplyer = killValue.endsWith('k') ? 1000 : killValue.endsWith('m') ? 1000000 : killValue.endsWith('b') ? 1000000000 : 1

        // remove k/m/b and commas on ubercheap KMs like velators (*cries in Pacifiers*)
        return multiplyer * killValue.replace(/\,/g,'').slice(0, -1)
    }

    function sum(values){
        return values.reduce((a, b) => a + b, 0)
    }

    // calculate stuff on page load
    calculateKms()
})();