Dead Frontier Value On Hover

Show only the total stack market value on hover in Dead Frontier Outpost.

Versione datata 26/04/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Dead Frontier Value On Hover
// @namespace    http://tampermonkey.net/
// @version      1.1
// @license      MIT
// @description  Show only the total stack market value on hover in Dead Frontier Outpost.
// @author       Zega
// @match        https://fairview.deadfrontier.com/*
// @grant        unsafeWindow
// ==/UserScript==

(function () {
    'use strict';

    // 1) Build ID→name map from page’s globalData
    const pageData = unsafeWindow.globalData || {};
    const itemNames = {};
    for (let id in pageData) {
        if (pageData[id] && pageData[id].name) {
            itemNames[id] = pageData[id].name;
        }
    }

    // 2) Tooltip styling
    const style = document.createElement('style');
    style.textContent = `
        .price-tooltip {
            position: absolute;
            background: rgba(0,0,0,0.85);
            color: #fff;
            padding: 6px 10px;
            font-size: 12px;
            border-radius: 6px;
            pointer-events: none;
            z-index: 9999;
            display: none;
        }
    `;
    document.head.appendChild(style);

    const tooltip = document.createElement('div');
    tooltip.className = 'price-tooltip';
    document.body.appendChild(tooltip);

    // 3) Attach hover listeners
    function attachListeners() {
        document.querySelectorAll('.item:not([data-hover-added])').forEach(el => {
            el.setAttribute('data-hover-added', 'true');

            el.addEventListener('mouseenter', async e => {
                const type  = el.getAttribute('data-type');
                const name  = itemNames[type] || el.getAttribute('data-name');
                const stack = parseInt(el.getAttribute('data-quantity')) || 1;
                if (!name || stack < 1) return;

                // fetch per-unit price
                const unit = await fetchUnitPrice(name);
                if (unit == null) {
                    tooltip.textContent = 'No listings';
                } else {
                    const total = unit * stack;
                    tooltip.textContent = `$${total.toFixed(2)} total`;
                }

                tooltip.style.left    = `${e.pageX + 12}px`;
                tooltip.style.top     = `${e.pageY + 12}px`;
                tooltip.style.display = 'block';
            });

            el.addEventListener('mouseleave', () => {
                tooltip.style.display = 'none';
            });
        });
    }

    const observer = new MutationObserver(attachListeners);
    observer.observe(document.body, { childList: true, subtree: true });
    attachListeners();

    // 4) Fetch best per-unit price, return number or null
    async function fetchUnitPrice(itemName) {
        const rawHash = getCookie('DeadFrontierFairview') || '';
        const hash    = decodeURIComponent(rawHash);
        const pagetime = Math.floor(Date.now() / 1000);
        const payload = {
            hash,
            pagetime,
            tradezone: 21,
            search: 'trades',
            searchtype: 'buyinglistitemname',
            searchname: itemName
        };

        try {
            const res  = await fetch('https://fairview.deadfrontier.com/onlinezombiemmo/trade_search.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'X-Requested-With': 'XMLHttpRequest',
                    'Referer': location.href,
                    'Origin':  location.origin
                },
                body: Object.entries(payload)
                    .map(([k,v])=>`${k}=${encodeURIComponent(v)}`)
                    .join('&')
            });
            const text = await res.text();
            const matches = [...text.matchAll(/tradelist_\d+_price=(\d+)&.*?tradelist_\d+_quantity=(\d+)/g)]
                .map(m => Number(m[1]) / Number(m[2]))
                .sort((a,b) => a - b);
            return matches.length ? matches[0] : null;
        } catch (err) {
            console.error('fetchUnitPrice error', err);
            return null;
        }
    }

    function getCookie(name) {
        const v = `; ${document.cookie}`;
        const parts = v.split(`; ${name}=`);
        return parts.length === 2 ? parts.pop().split(';')[0] : '';
    }
})();