The West Market Bid Calculator Light

Oblicza sumę wszystkich ofert na rynku po kliknięciu w zakładkę Sprzedaż

目前為 2025-03-25 提交的版本,檢視 最新版本

// ==UserScript==
// @name         The West Market Bid Calculator Light
// @namespace    http://tampermonkey.net/
// @version      2025-03-25
// @description  Oblicza sumę wszystkich ofert na rynku po kliknięciu w zakładkę Sprzedaż
// @author       Donald Kaczyński
// @include      https://*.the-west.*/game.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=the-west.pl
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let isListenerAdded = false;
    let lastTotal = 0;

    function resetState() {
        isListenerAdded = false;
        lastTotal = 0;
        const existingSum = document.querySelector('#market-sum');
        if (existingSum) {
            existingSum.remove();
        }
    }

    function formatMoney(amount) {
        return new Intl.NumberFormat('pl-PL').format(amount) + ' $';
    }

    function calculateAndDisplayTotal() {
        setTimeout(() => {
            const rows = document.querySelectorAll('.marketplace-sell .tbody .row');
            let total = 0;
            rows.forEach((row) => {
                const fetchMoneyIcon = row.querySelector('img[src*="fetch_money.png"]');
                if (fetchMoneyIcon) {
                    const bidCell = row.querySelector('.cell_4.mps_bid span');
                    if (bidCell && bidCell.textContent) {
                        const originalValue = bidCell.textContent.trim();
                        const value = originalValue.replace(/[^\d]/g, '');
                        const parsedValue = parseInt(value) || 0;
                        total += parsedValue;
                    }
                }
            });

            lastTotal = total;
            updateFooter(total);
        }, 500);
    }

    function updateFooter(total) {
        let footerCell = document.querySelector('.marketplace-sell .tfoot.statics .row_foot .cell_4.mps_bid');

        if (!footerCell) {
            const footerRow = document.querySelector('.marketplace-sell .tfoot.statics .row_foot');
            if (footerRow) {
                footerCell = footerRow.querySelector('.cell_4');
                if (!footerCell) {
                    footerCell = document.createElement('div');
                    footerCell.className = 'cell cell_4 mps_bid';
                    const cells = footerRow.querySelectorAll('.cell');
                    const cell4 = Array.from(cells).find(cell => cell.classList.contains('cell_4'));
                    if (cell4) {
                        cell4.parentNode.replaceChild(footerCell, cell4);
                    }
                }
            }
        }

        if (footerCell) {
            const existingSum = document.querySelector('#market-sum');
            if (existingSum) {
                existingSum.remove();
            }

            const headerBidCell = document.querySelector('.marketplace-sell .thead .row_head .cell_4.mps_bid');
            const cellPosition = headerBidCell ? headerBidCell.offsetLeft : 0;

            const sumDiv = document.createElement('div');
            sumDiv.id = 'market-sum';
            Object.assign(sumDiv.style, {
                position: 'absolute',
                bottom: '0px',
                left: cellPosition + 'px',
                textAlign: 'right',
                color: '#000000',
                fontWeight: 'bold',
                fontSize: '14px',
                zIndex: '1000'
            });

            const moneyStyle = `
                color: #00FF00;
                text-shadow:
                   -1px -1px 0 #000,
                    1px -1px 0 #000,
                   -1px  1px 0 #000,
                    1px  1px 0 #000;
                font-weight: bold;
                font-size: 16px;
            `;

            sumDiv.innerHTML = `SUMA SPRZEDAŻY: <span style="${moneyStyle}">${formatMoney(total)}</span>`;

            const parent = footerCell.closest('.tfoot');
            if (parent) {
                parent.style.position = 'relative';
                parent.appendChild(sumDiv);
            }
        }
    }

    function setupTabListener() {
        const marketWindow = document.querySelector('.marketplace-sell');
        if (!marketWindow || !marketWindow.offsetParent) {
            return;
        }

        const allTabs = document.querySelectorAll('.tw2gui_window_tab_text');
        const sellTab = Array.from(allTabs).find(tab => tab.textContent.includes('Sprzedaż'));

        if (sellTab) {
            sellTab.removeEventListener('click', calculateAndDisplayTotal);
            sellTab.addEventListener('click', calculateAndDisplayTotal);

            const tableObserver = new MutationObserver(() => {
                if (sellTab.closest('.tw2gui_window_tab_active')) {
                    calculateAndDisplayTotal();
                }
            });

            const tableBody = document.querySelector('.marketplace-sell .tbody');
            if (tableBody) {
                tableObserver.observe(tableBody, {
                    childList: true,
                    subtree: true
                });
            }

            if (sellTab.closest('.tw2gui_window_tab_active')) {
                calculateAndDisplayTotal();
            }

            isListenerAdded = true;
        }
    }

    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            const marketWindow = document.querySelector('.marketplace-sell');

            if (marketWindow && marketWindow.offsetParent && !isListenerAdded) {
                setupTabListener();
                break;
            }

            if (!marketWindow || !marketWindow.offsetParent) {
                resetState();
            }
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['style', 'class']
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址