vitaminb16 / Live Counting AHK

// ==UserScript==
// @name         Live Counting AHK
// @namespace    http://tampermonkey.net/
// @version      0.2.2
// @description  try to take over the world!
// @author       VitaminB16
// @match        *://*.reddit.com/live/*
// @license      MIT
// @updateURL    https://openuserjs.org/meta/vitaminb16/Live_Counting_AHK.meta.js
// ==/UserScript==

// does not require LCE. If LCE is running, make sure NOT to set `Submit Shortcut` to `Enter`. Set it either to `Off` or to `Ctrl+Enter`

// 17/May/22 added submit-shortcut selector, fixed bug where non-counts would persist after sending
// 18/Aug/22 added anti bar spam functionality – on by default
// 02/Dec/22 changes of 18 Aug are now off by default

$(document).ready(function () { // When document has loaded
    var make_LC_better = 0

    // settings selector with submit-shortcut options
    var AHKSettings = '<h1 style="font-size:16px;"><a id="ahk_settings" href="https://openuserjs.org/scripts/vitaminb16/Live_Counting_AHK" target="_blank">Tampermonkey AHK (off)</a><br><p for="ahk_shortcut">Send shortcut: <select name="ahk_shortcut" id="ahk_shortcut"><option value="ahk_enter">Enter</option><option value="ahk_space">Space</option><option value="ahk_shift">Shift</option><option value="ahk_control">Control</option><option value="ahk_alt">Option (Alt)</option><option value="ahk_cmd">Cmd</option><option value="ahk_tab">Tab</option><option value="ahk_esc">Escape</option></select></p><p for="improve_lc">Improve LC: <label class="switch"><input class="improve_lc" type="checkbox" name="improve_lc"></label></p></h1>'
    setTimeout(function () {
        jQuery('#lceversion').before(AHKSettings)
        var saved_ahk_shortcut = sessionStorage.getItem('ahk_setting_shortcut')
        var ahk_selector = document.querySelector('#ahk_shortcut')
        var improve_LC_selector = document.querySelector('.improve_lc')
        if (saved_ahk_shortcut != null) {ahk_selector.value = saved_ahk_shortcut}
        var ahk_dict = {'ahk_enter':13, 'ahk_space':32, 'ahk_shift':16, 'ahk_control':17, 'ahk_alt':18, 'ahk_tab':9, 'ahk_esc':27, 'ahk_cmd':91}
        var ahk_shortcut = ahk_dict[ahk_selector.value]

        ahk_selector.onchange = function(){
            sessionStorage.setItem('ahk_setting_shortcut', ahk_selector.value)
            ahk_shortcut = ahk_dict[ahk_selector.value]
        };/*
        improve_LC_selector.onchange = function(){
            make_LC_better = 1 - make_LC_better
        };
        improve_LC_selector.click() // comment this out if you don't want LC to be a better place.
        */

        var sent = 0;
        var text = "";
        var newText = sessionStorage.getItem('count');
        var state = 0;
        var active = sessionStorage.getItem('active');

        if (active == null) {active = 0}

        function improveCountingExperience(x) {
            x = x.replaceAll(/(agoncbg|goncbg|i cant fucking hp?|i cant fucking hp|i cant fucking|i cant hp?|i cant hp|chalupa exacta|chalupa latexacta|trifecta!!!!!|trifecta!!!!|trifecta!!!|trifecta!!|trifecta!|trifectas|trifecta|trejexacta|exacta!!!|exacta!!|exacta!|exacta|antibar|bar)/ig, '')
            return(x)
        }

        function makeLC_Better() {
            let p = document.querySelectorAll('p')
            let n_p = p.length
            let i = 0
            for (i=0; i < n_p; i++)
            {
                p[i].innerHTML = improveCountingExperience(p[i].innerHTML)
            }
        }

        function removeStuff() {
            if (active == 1) {
                document.querySelectorAll('.liveupdate.pending-embed.stricken').forEach(x => x.remove())
            }
        }

        // `cuteballgames` hide stricken counts. uncomment this line if you want to hide stricken counts `cuteballgames`
        // var intervalId = window.setInterval(function() {removeStuff()}, 400);

        // hide every mention of 'trifecta' and its various slippery variations
        var intervalId = window.setInterval(function() {if (make_LC_better == 1) {makeLC_Better()}}, 500);

        document.addEventListener("keypress", function (e) {
            // when textarea is empty, paste the stored count (e.g. 27,852,8)
            var textArea = document.querySelectorAll('.usertext-edit.md-container textarea')[0]
            if ((textArea.value == "" || textArea.value == "\n") && active == 1) {
                textArea.value = sessionStorage.getItem('count')
            }
        });

        document.addEventListener("keydown", function (e) {
            // right arrow to send pincus {:}
            var textArea = document.querySelectorAll('.usertext-edit.md-container textarea')[0]
            if (e.keyCode == 39 && active == 1) {
                textArea.value = textArea.value + " {:}"
            }
        });

        document.addEventListener("keypress", function (e) {
            if ([13, 32].includes(ahk_shortcut)) // if enter or space is the shortcut
            {
                // main part of the script
                var object = document.querySelectorAll('.save-button button')[0]
                var textArea = document.querySelectorAll('.usertext-edit.md-container textarea')[0]
                if ((e.keyCode == parseInt(ahk_shortcut)) && active == 1) { // when enter is pressed
                    let t = (textArea.value) // take what is in the text area (e.g. 27,852,852)
                    let oldText = sessionStorage.getItem('count'); // take the last stored count (e.g. 27,852,8)
                    if (t == "") {
                        textArea.value = oldText
                    } // this is the equivalent of NoClear
                    t = textArea.value
                    newText = sessionStorage.getItem('count');
                    if ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9].toString().includes(t[0])) { // if the first character is an integer
                        let itemToPaste = t.slice(0, 8)
                        sessionStorage.setItem('count', itemToPaste); // save the first 9 characters (e.g. 27,852,8)
                        object.click() // send update
                        sent = 0
                        setTimeout(function () {
                            textArea.value = itemToPaste;
                            textArea.focus();
                        }, 50); // paste and refocus
                    }
                    else {
                        object.click() // send update
                        setTimeout(function () {
                            textArea.value = itemToPaste;
                            textArea.focus();
                        }, 50); // paste and refocus
                    }
                }
            }
        });
        // same, but with keydown insteadd of keypress
        document.addEventListener("keydown", function (e) {
            if (![13, 32].includes(ahk_shortcut)) // if anything else is the shortcut
            {
                // main part of the script
                var object = document.querySelectorAll('.save-button button')[0]
                var textArea = document.querySelectorAll('.usertext-edit.md-container textarea')[0]
                if ((e.keyCode == parseInt(ahk_shortcut)) && active == 1) { // when enter is pressed
                    let t = (textArea.value) // take what is in the text area (e.g. 27,852,852)
                    let oldText = sessionStorage.getItem('count'); // take the last stored count (e.g. 27,852,8)
                    if (t == "") {
                        textArea.value = oldText
                    } // this is the equivalent of NoClear
                    t = textArea.value
                    newText = sessionStorage.getItem('count');
                    if ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9].toString().includes(t[0])) { // if the first character is an integer
                        let itemToPaste = t.slice(0, 8)
                        sessionStorage.setItem('count', itemToPaste); // save the first 9 characters (e.g. 27,852,8)
                        object.click() // send update
                        sent = 0
                        setTimeout(function () {
                            textArea.value = itemToPaste;
                            textArea.focus();
                        }, 50); // paste and refocus
                    }
                    else {
                        object.click() // send update
                        setTimeout(function () {
                            textArea.value = itemToPaste;
                            textArea.focus();
                        }, 50); // paste and refocus
                    }
                }
            }
        });

        // press `=` to activate; `-` to deactivate
        document.addEventListener("keypress", function (e) {
            var textArea = document.querySelectorAll('.usertext-edit.md-container textarea')[0]
            if (event.keyCode == 61) {
                active = 1
                // setTimeout(function () {textArea.value = sessionStorage.getItem('count')}, 300)
                document.querySelector('#ahk_settings').textContent = "Tampermonkey AHK (on)"
            }
            else if (event.keyCode == 45) {
                active = 0
                document.querySelector('#ahk_settings').textContent = "Tampermonkey AHK (off)"
            }
        });

        var intervalId2 = window.setInterval(function () {
            // (this might be unnecessary, but here for extra safety)
            // if a count was just sent and text box is empty, paste the saved count
            var textArea = document.querySelectorAll('.usertext-edit.md-container textarea')[0]

            if ((textArea.value == "" || textArea.value == "\n") && active == 1) {
                // paste when the count was sent
                textArea.value = sessionStorage.getItem('count');
                sent = 1
            }
        }, 200)
        }, 2000)
});