Dead Frontier Market Value on Hover

Show market prices on hover for ammo and singular items like Repair Kits and Easter Eggs.

Verze ze dne 25. 04. 2025. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         Dead Frontier Market Value on Hover
// @namespace    http://tampermonkey.net/
// @version      1.1
// @license MIT
// @description  Show market prices on hover for ammo and singular items like Repair Kits and Easter Eggs.
// @author       Zega
// @match        https://fairview.deadfrontier.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const itemNames = {
        "repairkit": "Repair Kit",
        "biomassammo": "Biomass",
      "20gaugeammo": "20 gauge shells",
      "16gaugeammo": "16 gauge shells",
      "10gaugeammo": "10 gauge shells",
      "12gaugeammo": "12 gauge shells",
      "38ammo": ".38 Handgun Bullets",
      "40ammo": ".40 Handgun Bullets",
      "45ammo": ".45 Handgun Bullets",
      "50ammo": ".50 Handgun Bullets",
      "fuelammo": "Gasoline",
      "55ammo": ".55 Handgun Bullets",
      "35ammo": "9mm Handgun Bullets",
      "357ammo": ".357 Handgun Bullets",
        "32ammo": ".32 Handgun Bullets",
      "9rifleammo": "9mm Rifle Bullets",
       "55rifleammo": "5.5mm Rifle Bullets",
       "75rifleammo": "7.5mm Rifle Bullets",
       "127rifleammo": "12.7mm Rifle Bullets",
       "14rifleammo": "14mm Rifle Bullets",
      "heavygrenadeammo": "Heavy Grenades",
      "grenadeammo": "grenades",
      "energycellammo": "Energy Cell",
      "nerotonin8b": "Nerotonin 8b",
      "inhaler": "Inhaler",
        "nerotonin5a": "Nerotonin 5A",
        "steroids": "Steroids",
        "woodenplanks": "Wooden Planks",
        "nails": "Nails",
        "energybar": "Energy Bar",
        "whiskey": "whiskey",
        "driedtruffles": "Dried Truffles",
        "meatjerky": "Meat Jerky",
      
        "easteregg2025": "Easter Egg 2025", 
      "trickortreat2024": "Trick Or Treat 2024", // Add easter egg or any singular item here
        // Add more item mappings as needed
    };

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

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

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

            item.addEventListener('mouseenter', e => {
                const type = item.getAttribute('data-type');
                const nameAttr = item.getAttribute('data-name');
                const name = itemNames[type] || nameAttr;

                console.log("Hovered item:", { type, name });

                const stack = parseInt(item.getAttribute('data-quantity')) || 1; // Default to 1 for singular items
                if (name && stack > 0) fetchAndShowPrice(name, e, stack);
            });

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

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

    function fetchAndShowPrice(itemName, event, stackCount) {
        const hash = getCookie('DeadFrontierFairview');
        const pagetime = Math.floor(Date.now() / 1000);

        const payload = {
            hash,
            pagetime,
            tradezone: 21,
            searchname: itemName,
            memID: '',
            profession: '',
            category: '',
            search: 'trades',
            searchtype: 'buyinglistitemname'
        };

        console.log("Fetching price for:", itemName); 

        fetch('https://fairview.deadfrontier.com/onlinezombiemmo/trade_search.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: Object.entries(payload).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('&')
        })
        .then(res => res.text())
        .then(text => {
            console.log("Market Response: ", text); 
            const msg = parseMarketResponse(text, stackCount, itemName);
            showTooltip(msg, event);
        })
        .catch(err => {
            console.error("Fetch error:", err);
            showTooltip("Error fetching price", event);
        });
    }

    function parseMarketResponse(html, stackCount, itemName) {
        console.log("Raw HTML Response:", html);

        const regex = /tradelist_\d+_price=(\d+)&.*?tradelist_\d+_quantity=(\d+)/g;
        const entries = [];
        let match;

        while ((match = regex.exec(html)) !== null) {
            const price = parseInt(match[1]);
            const quantity = parseInt(match[2]);
            if (quantity > 0) {
                const perUnit = price / quantity;
                entries.push({ price, quantity, perUnit });
            }
        }

        console.log("Parsed Market Entries: ", entries);

        if (entries.length === 0) {
            console.log("No listings found for the item");
            return "Price not available";
        }

        entries.sort((a, b) => a.perUnit - b.perUnit);
        const best = entries[0];

        // For singular items like Repair Kit or Easter Egg
        if (stackCount === 1) {
            return `Value: $${best.price.toFixed(2)}`;
        }

        const totalValue = (best.perUnit * stackCount).toFixed(2);
        console.log("Total Value for Stack:", totalValue);

        return `Value: $${totalValue}`;
    }

    function showTooltip(msg, e) {
        tooltip.textContent = msg;
        tooltip.style.left = `${e.pageX + 12}px`;
        tooltip.style.top = `${e.pageY + 12}px`;
        tooltip.style.display = 'block';
    }

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

    attachListeners();
})();