DayZ Map Improvement

6/11/2024, 1:26:23 AM

// ==UserScript==
// @name        DayZ Map Improvement
// @namespace   https://azzurite.tv
// @match       https://dayz.xam.nu/*
// @run-at      document-start
// @grant       none
// @version     1.3.4
// @author      Azzurite
// @license     GPLv3
// @description 6/11/2024, 1:26:23 AM
// ==/UserScript==

(() => {

    function makeHuntingMoreVisible() {
        if (this.options.iconUrl === `https://dayz.xam.nu/js/../images/b8fe30b7.webp`) {
            this.options.iconUrl = `https://images.azzurite.tv/uploads/original/d3/0f/6a50614db7df56f32d5aa2840268.webp`;
        } else if (this.options?.iconUrl === `https://dayz.xam.nu/js/../images/a92e2f53.webp`) {
            this.options.iconUrl = `https://images.azzurite.tv/uploads/original/92/8b/e706a502ab0931c2338883333633.webp`;
        }
    }

    Object.defineProperty(window, `L`, {
        set(obj) {
            this.storedL = obj;
            obj.Map.addInitHook(function() { // addInitHook binds `this`
                window.map = this;
            });
            obj.Icon.addInitHook(makeHuntingMoreVisible);
        },
        get() {
            return this.storedL;
        }
    })

    function anyMarkerActive() {
        return document.querySelector(`.filters > button`).dataset[`active`] === `1`;
    }

    function toggle(type, subtype) {
        const btn = document.querySelector(`[data-type="${type}"][data-weight="${subtype}"]`);
        btn?.click();
    }

    function toggleTrees() {
        [`pear`, `apple`, `plum`].map(tree => toggle(`food`, tree));
    }

    function toggleSecondaryPlayerSpawns() {
        [`playerspawnhop`, `playerspawnsafe`, `playerspawntravel`].map(spawn => toggle(`misc`, spawn));
    }

    function toggleCarSpawns() {
        [`vehicleoffroadhatchback`, `vehiclehatchback02`, `vehicleoffroad02`, `vehicletruck01`, `vehicleciviliansedan`, `vehiclesedan02`].map(car => toggle(`vehicle`, car));
    }

    function toggleAll() {
        document.querySelector(`.filters > button`).click();
    }

    function getButtonList() {
        return document.querySelector(`.filters`).parentNode;
    }

    function nicerToggleAll() {
        toggleAll();
        if (anyMarkerActive()) {
            toggleTrees();
            toggle(`animal`, `all`);
            toggle(`area`, `dynamiccontamination`);
            toggle(`misc`, `infected`);
            toggleSecondaryPlayerSpawns();
            toggleCarSpawns();
        }
    }

    function replaceFilterAllButton() {
        const li = document.createElement(`li`);
        li.classList.add(`azzu-all`);
        const btn = document.querySelector(`[data-type="all"][data-weight="all"]`).cloneNode(true);
        li.append(btn);
        btn.addEventListener(`click`, () => {
            stopHighlighters();
            nicerToggleAll();
            if (anyMarkerActive()) {
                document.querySelector(`.azzu-all > button`).dataset.active = `1`;
            } else {
                document.querySelector(`.azzu-all > button`).dataset.active = `0`;
            }
        });
        getButtonList().prepend(li);
        document.querySelector(`.filters`).style.display = `none`;
    }

    function createTextButton(text) {
        const li = document.createElement(`li`);
        li.classList.add(`azzu-highlight`);
        const btn = document.createElement(`button`);
        btn.style.padding = `0 10px`;
        btn.style.width = `inherit`;
        btn.innerHTML = text;
        li.append(btn);
        getButtonList().prepend(li);
        return btn;
    }

    const highlighters = {};
    function stopHighlighters() {
        for (const [key, {timeout, onStopHighlight}] of Object.entries(highlighters)) {
            if (timeout) {
                onStopHighlight();
                clearTimeout(timeout);
                highlighters[key].timeout = false;
            }
        }
    }

    function isHightlighterRunning(id) {
        return !!highlighters[id].timeout;
    }

    function addBlinkingBehavior(btn, id, highlight, onStopHighlight = () => {}, onStartHighlight = () => {}) {
        highlighters[id] = { timeout: false, onStopHighlight };
        let curBlink = false;
        btn.addEventListener(`contextmenu`, (ev) => {
            ev.preventDefault();

            const running = isHightlighterRunning(id);
            stopHighlighters();
            if (running) {
                return;
            }

            onStartHighlight();

            const doHighlight = () => {
                if (curBlink) {
                    console.log(`highlight off`);
                    highlight();
                    highlighters[id].timeout = setTimeout(doHighlight, 100);
                } else {
                    console.log(`highlight on`);
                    highlight();
                    highlighters[id].timeout = setTimeout(doHighlight, 600);
                }
                curBlink = !curBlink;
            }
            highlighters[id].timeout = setTimeout(doHighlight, 0);
        });
        btn.addEventListener(`click`, (ev) => {
            if (!ev.isTrusted) {
                return;
            }
            const running = isHightlighterRunning(id);
            stopHighlighters();
        })
    }

    function createHighlightButton(text, highlight) {
        const btn = createTextButton(text);
        btn.classList.add(`highlighter`);
        btn.classList.add(text);
        btn.dataset.active = `0`;

        btn.addEventListener(`click`, (ev) => {
            if (btn.dataset.active === `1`) {
                toggleAll();
                nicerToggleAll();
                btn.dataset.active = `0`;
            } else {
                if (anyMarkerActive()) {
                    toggleAll();
                }
                highlight();
                btn.dataset.active = `1`;
            }
        });

        addBlinkingBehavior(btn, text, highlight);
    }

    function addWeaponAmmoHighlight() {
        createHighlightButton(`HighValue`, () => {
            toggle(`medic`, `all`);
            toggle(`hunting`, `high`);
            toggle(`hunting`, `medium`);
            toggle(`hunting`, `low`);
            toggle(`hunting`, `store`);
            toggle(`police`, `all`);
            toggle(`military`, `all`);
        });
    }

    function addShowCarSpawns() {
        createHighlightButton(`Vehicles`, toggleCarSpawns);
    }

    function addBlinkingToAllButtons() {
        for (const btn of [...document.querySelectorAll(`[data-weight][data-type]:not([data-type="all"])`)]) {
            const {type, weight} = btn.dataset;
            const id = `${type}-${weight}`;
            btn.classList.add(`highlighter`);
            btn.classList.add(id);
            addBlinkingBehavior(btn, id, () => btn.click(), () => btn.click());
        }
    }

    function init() {
        if (!document.querySelector(`.filters`)) {
            setTimeout(init, 100);
            return;
        }

        replaceFilterAllButton();
        addBlinkingToAllButtons();
        addWeaponAmmoHighlight();
        addShowCarSpawns();
    }

    setTimeout(init, 100);
})();