Bazaar Sentry OG

Full feature set: Price/range highlighting, Blacklist, Dual-tab settings, and PDA-stable links with mobile keyboard fixes.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

Advertisement:

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

Advertisement:

// ==UserScript==
// @name         Bazaar Sentry OG
// @namespace    torn.bazaar_sentry
// @version      2.3.0
// @description  Full feature set: Price/range highlighting, Blacklist, Dual-tab settings, and PDA-stable links with mobile keyboard fixes.
// @author       BBSmalls [3908857]
// @match        *://www.torn.com/bazaar.php*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // --- DATA & STORAGE ---
    const defaultPrices = [{ amt: 1, max: null, enabled: true, color: "#2ecc71" }, { amt: 2, max: null, enabled: true, color: "#2ecc71" }];
    const defaultIgnored = [{ name: "Baseball Bat", enabled: false }];

    let userSettings = JSON.parse(localStorage.getItem('bbsmalls_sentry_list_v2')) || defaultPrices;
    let rawIgnored = JSON.parse(localStorage.getItem('bbsmalls_sentry_ignored_v1')) || defaultIgnored;
    let ignoredItems = rawIgnored.map(item => (typeof item === 'string' ? { name: item, enabled: true } : item));

    const savePrices = () => localStorage.setItem('bbsmalls_sentry_list_v2', JSON.stringify(userSettings));
    const saveIgnored = () => localStorage.setItem('bbsmalls_sentry_ignored_v1', JSON.stringify(ignoredItems));

    const hexToRgba = (hex, alpha = 0.25) => {
        const r = parseInt(hex.slice(1, 3), 16), g = parseInt(hex.slice(3, 5), 16), b = parseInt(hex.slice(5, 7), 16);
        return `rgba(${r}, ${g}, ${b}, ${alpha})`;
    };

    let settingsPanel = null;
    let currentTab = 'prices';
    let bannerDismissed = false;

    // --- SHADOW DOM BANNER ---
    let host = document.getElementById('sentry-banner-host');
    if (!host) {
        host = document.createElement('div');
        host.id = 'sentry-banner-host';
        document.documentElement.appendChild(host);
        host.attachShadow({mode: 'open'});
    }

    const updateFloatingBanner = (isFound) => {
        const root = host.shadowRoot;
        if (!isFound) { bannerDismissed = false; root.innerHTML = ''; return; }
        if (bannerDismissed || root.innerHTML !== '') return;
        root.innerHTML = `<style>.w{position:fixed;top:0;left:0;width:100%;background:#e74c3c;color:white;display:flex;align-items:center;justify-content:center;padding:12px 0;z-index:2147483647;box-shadow:0 2px 10px rgba(0,0,0,0.5);pointer-events:none;font-family:Arial;font-weight:bold;}.t{font-size:16px;text-transform:uppercase;letter-spacing:2px;}.b{position:absolute;right:20px;cursor:pointer;font-size:24px;pointer-events:auto;padding:0 10px;}</style><div class="w"><span class="t">⚠️ Item(s) Detected ⚠️</span><span class="b" id="c">×</span></div>`;
        root.getElementById('c').onclick = () => { bannerDismissed = true; root.innerHTML = ''; };
    };

    // --- MODAL ENGINE (Mobile Keyboard Optimized, Multi-Input Capable) ---
    const showModal = (title, contentHTML, onConfirm) => {
        const overlay = document.createElement('div');
        overlay.style.cssText = `position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);z-index:2000000;display:flex;align-items:center;justify-content:center;font-family:Arial,sans-serif;`;
        const modal = document.createElement('div');
        modal.style.cssText = `background:#222;border:3px solid #666;border-radius:8px;width:220px;overflow:hidden;box-shadow:0 0 30px rgba(0,0,0,0.5);`;
        modal.innerHTML = `
            <div style="background:#333;padding:12px;font-weight:bold;font-size:14px;color:#fff;text-align:center;border-bottom:1px solid #444;">${title}</div>
            <div style="padding:15px;color:#ddd;font-size:13px;text-align:center;">
                ${contentHTML}
                <div id="modal-error" style="color:#e74c3c;font-size:11px;margin-top:8px;font-weight:bold;min-height:14px;"></div>
            </div>
            <div style="display:flex;border-top:1px solid #444;">
                <button id="m-cancel" style="flex:1;padding:10px;background:#333;color:#aaa;border:none;cursor:pointer;pointer-events:auto;">Cancel</button>
                <button id="m-ok" style="flex:1;padding:12px;background:#27ae60;color:#fff;border:none;cursor:pointer;font-weight:bold;border-left:1px solid #444;pointer-events:auto;">OK</button>
            </div>`;

        overlay.appendChild(modal);
        document.body.appendChild(overlay);

        const errorEl = modal.querySelector('#modal-error'),
              inputs = Array.from(modal.querySelectorAll('input')),
              btnOk = modal.querySelector('#m-ok'),
              btnCancel = modal.querySelector('#m-cancel');

        const handleConfirm = (e) => {
            if (e) { e.preventDefault(); e.stopPropagation(); }
            inputs.forEach(inp => inp.blur()); // Dismisses mobile keyboard

            const setError = (msg) => {
                errorEl.innerText = msg;
                modal.animate([{transform:'translateX(-5px)'},{transform:'translateX(5px)'}],{duration:100,iterations:2});
            };

            // Single input -> raw value (back-compat). Multiple inputs -> array of values. No inputs -> true.
            const val = inputs.length > 1 ? inputs.map(i => i.value) : (inputs.length === 1 ? inputs[0].value : true);
            if (onConfirm(val, setError) !== false) overlay.remove();
        };

        if (inputs.length) {
            setTimeout(() => inputs[0].focus(), 10);
            inputs.forEach(inp => {
                inp.onkeydown = (e) => { if (e.key === 'Enter') handleConfirm(e); else errorEl.innerText = ""; };
            });
        }

        btnOk.addEventListener('click', handleConfirm);
        btnOk.addEventListener('touchstart', handleConfirm, {passive: false});
        btnCancel.onclick = () => overlay.remove();
        btnCancel.addEventListener('touchstart', (e) => { e.preventDefault(); overlay.remove(); }, {passive: false});
    };

    // --- SETTINGS PANEL ---
    const createSettingsPanel = () => {
        if (settingsPanel) settingsPanel.remove();
        userSettings.sort((a,b) => {
            if (a.amt !== b.amt) return a.amt - b.amt;
            const aHasMax = a.max != null, bHasMax = b.max != null;
            if (aHasMax !== bHasMax) return aHasMax ? 1 : -1; // no-max entries sort before ranged ones at the same min
            if (aHasMax && bHasMax) return a.max - b.max;
            return 0;
        });
        ignoredItems.sort((a,b)=>a.name.localeCompare(b.name));
        settingsPanel = document.createElement('div');
        settingsPanel.style.cssText = `position:fixed;top:50%;left:50%;transform:translate(-50%,-50%);background:#222;color:white;border-radius:8px;z-index:1000001;width:300px;font-family:Arial,sans-serif;border:3px solid #666;display:none;overflow:hidden;box-shadow:0 4px 20px rgba(0,0,0,0.6);`;
        const tabs = `<div style="display:flex;background:#333;border-bottom:1px solid #444;"><div id="t-p" style="flex:1;padding:10px;text-align:center;cursor:pointer;font-size:12px;font-weight:bold;${currentTab==='prices'?'background:#222;border-bottom:2px solid #27ae60;':''}">Prices</div><div id="t-i" style="flex:1;padding:10px;text-align:center;cursor:pointer;font-size:12px;font-weight:bold;${currentTab==='ignored'?'background:#222;border-bottom:2px solid #27ae60;':''}">Blacklist</div></div>`;
        const ROW = `margin-bottom:6px;display:flex;align-items:center;gap:12px;background:#333;padding:0 10px;border-radius:4px;height:42px;box-sizing:border-box;`;
        const LBL = `font-size:13px;flex-grow:1;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;font-weight:500;line-height:42px;cursor:pointer;`;
        const DEL = `cursor:pointer;color:#e74c3c;font-weight:bold;font-size:22px;line-height:42px;width:20px;text-align:center;flex-shrink:0;`;

        const fmtPrice = (item) => `$${item.amt.toLocaleString()}${item.max != null ? ' - $' + item.max.toLocaleString() : ''}`;

        let html = (currentTab==='prices')
            ? userSettings.map((item,i)=>`<div style="${ROW}"><span class="p-d" data-i="${i}" style="${DEL}">×</span><input type="checkbox" class="p-t" data-i="${i}" ${item.enabled?'checked':''} style="width:20px;height:20px;flex-shrink:0;"><input type="color" class="p-c" data-i="${i}" value="${item.color}" style="width:24px;height:24px;border:none;background:none;padding:0;flex-shrink:0;"><label class="p-e" data-i="${i}" style="${LBL}">${fmtPrice(item)}</label></div>`).join('')
            : ignoredItems.map((item,i)=>`<div style="${ROW}"><span class="i-d" data-i="${i}" style="${DEL}">×</span><input type="checkbox" class="i-t" data-i="${i}" ${item.enabled?'checked':''} style="width:20px;height:20px;flex-shrink:0;"><label class="i-e" data-i="${i}" style="${LBL}">${item.name}</label></div>`).join('');

        settingsPanel.innerHTML = `<div style="background:#444;padding:10px;text-align:center;font-weight:bold;font-size:13px;">Bazaar Sentry</div>${tabs}<div style="padding:12px;"><div style="height:205px;overflow-y:auto;">${html}</div><div style="display:flex;gap:8px;margin-top:10px;"><button id="add-b" style="padding:4px 12px;background:#27ae60;color:white;border:none;cursor:pointer;font-size:18px;border-radius:4px;">+</button><button id="close-b" style="flex-grow:1;background:#34495e;color:white;border:none;cursor:pointer;font-weight:bold;border-radius:4px;">Close</button></div></div>`;
        document.body.appendChild(settingsPanel);

        document.getElementById('t-p').onclick = () => { currentTab='prices'; createSettingsPanel(); settingsPanel.style.display='block'; };
        document.getElementById('t-i').onclick = () => { currentTab='ignored'; createSettingsPanel(); settingsPanel.style.display='block'; };
        document.getElementById('close-b').onclick = () => settingsPanel.style.display='none';

        // Shared validator for the min/max price modal. Returns {min,max} or null (and calls err) on failure.
        const parsePriceInputs = (vals, err, skipIdx) => {
            const [minRaw, maxRaw] = vals;
            const min = parseInt(String(minRaw).replace(/[^0-9]/g,''), 10);
            if (!min) { err("Invalid Min!"); return null; }
            let max = null;
            if (maxRaw && maxRaw.trim() !== '') {
                max = parseInt(String(maxRaw).replace(/[^0-9]/g,''), 10);
                if (!max || max <= min) { err("Max must be > Min!"); return null; }
            }
            if (userSettings.some((it,n) => n != skipIdx && it.amt === min && (it.max ?? null) === max)) { err("Duplicate Entry!"); return null; }
            return { min, max };
        };

        const priceModalContent = (item) => `<div style="text-align:left;font-size:11px;color:#aaa;margin-bottom:4px;">Min Price</div><input type="text" class="price-fmt-input" value="${item ? '$' + item.amt.toLocaleString() : ''}" placeholder="$0" style="width:100%;padding:5px;text-align:center;box-sizing:border-box;"><div style="text-align:left;font-size:11px;color:#aaa;margin:8px 0 4px;">Max Price (optional)</div><input type="text" class="price-fmt-input" value="${item && item.max != null ? '$' + item.max.toLocaleString() : ''}" placeholder="none" style="width:100%;padding:5px;text-align:center;box-sizing:border-box;">`;
const attachPriceFormatting = () => {
    document.querySelectorAll('.price-fmt-input').forEach(inp => {
        inp.addEventListener('input', () => {
            const start = inp.selectionStart;
            const digitsBeforeCursor = inp.value.slice(0, start).replace(/[^0-9]/g, '').length;
            const digits = inp.value.replace(/[^0-9]/g, '');
            inp.value = digits ? '$' + parseInt(digits, 10).toLocaleString() : '';
            let pos = digitsBeforeCursor === 0 ? (inp.value ? 1 : 0) : inp.value.length;
            let count = 0;
            for (let i = 0; i < inp.value.length; i++) {
                if (/[0-9]/.test(inp.value[i])) count++;
                if (count === digitsBeforeCursor) { pos = i + 1; break; }
            }
            inp.setSelectionRange(pos, pos);
        });
    });
};
        if(currentTab==='prices'){
            settingsPanel.querySelectorAll('.p-t').forEach(el=>el.onchange=(e)=>{userSettings[e.target.dataset.i].enabled=e.target.checked;savePrices();});
            settingsPanel.querySelectorAll('.p-c').forEach(el=>el.onchange=(e)=>{userSettings[e.target.dataset.i].color=e.target.value;savePrices();});
            settingsPanel.querySelectorAll('.p-d').forEach(el=>el.onclick=(e)=>{const idx=e.target.dataset.i;showModal("Delete",`Remove ${fmtPrice(userSettings[idx])}?`,()=>{userSettings.splice(idx,1);savePrices();createSettingsPanel();settingsPanel.style.display='block';});});
            settingsPanel.querySelectorAll('.p-e').forEach(el=>el.onclick=(e)=>{
                const idx=e.target.dataset.i;
                showModal("Edit Price", priceModalContent(userSettings[idx]), (v,err)=>{
                    const parsed = parsePriceInputs(v, err, idx);
                    if (!parsed) return false;
                    userSettings[idx].amt = parsed.min;
                    userSettings[idx].max = parsed.max;
                    savePrices();createSettingsPanel();settingsPanel.style.display='block';
                });
                attachPriceFormatting();
            });
        } else {
            settingsPanel.querySelectorAll('.i-t').forEach(el=>el.onchange=(e)=>{ignoredItems[e.target.dataset.i].enabled=e.target.checked;saveIgnored();});
            settingsPanel.querySelectorAll('.i-d').forEach(el=>el.onclick=(e)=>{const idx=e.target.dataset.i;showModal("Delete",`Remove "${ignoredItems[idx].name}"?`,()=>{ignoredItems.splice(idx,1);saveIgnored();createSettingsPanel();settingsPanel.style.display='block';});});
            settingsPanel.querySelectorAll('.i-e').forEach(el=>el.onclick=(e)=>{const idx=e.target.dataset.i;showModal("Edit Name",`<input type="text" value="${ignoredItems[idx].name}" style="width:150px;padding:5px;text-align:center;">`,(v,err)=>{const n=v.trim(); if(!n){err("Empty!");return false;} if(ignoredItems.some((it,m)=>it.name.toLowerCase()===n.toLowerCase() && m!=idx)){err("Duplicate Entry!");return false;} ignoredItems[idx].name=n;saveIgnored();createSettingsPanel();settingsPanel.style.display='block';});});
        }
        document.getElementById('add-b').onclick=()=>{
            if(currentTab==='prices'){
                showModal("Add Price", priceModalContent(null), (v,err)=>{
                    const parsed = parsePriceInputs(v, err, -1);
                    if (!parsed) return false;
                    userSettings.push({amt:parsed.min, max:parsed.max, enabled:true, color:"#2ecc71"});
                    savePrices();createSettingsPanel();settingsPanel.style.display='block';
                });
                attachPriceFormatting();
            }
            else { showModal("Blacklist",`<input type="text" placeholder="Item Name" style="width:150px;padding:5px;text-align:center;">`,(v,err)=>{const n=v.trim(); if(!n){err("Empty!");return false;} if(ignoredItems.some(it=>it.name.toLowerCase()===n.toLowerCase())){err("Duplicate Entry!");return false;} ignoredItems.push({name:n,enabled:true});saveIgnored();createSettingsPanel();settingsPanel.style.display='block';});}
        };
    };

    // --- STABLE PDA LINK ---
    const injectSettingsLink = () => {
        if (document.getElementById('sentry-settings-link')) return;
        const target = document.querySelector('div[class*="titleContainer"]');
        if (target) {
            const l = document.createElement('div');
            l.id = 'sentry-settings-link';
            l.style.cssText = `margin-left:15px;cursor:pointer;color:#b0b0b0;font-size:13px;display:inline-flex;align-items:center;gap:4px;z-index:999999;pointer-events:auto !important;`;
            l.innerHTML = `🔥<span style="text-decoration:underline;">Bazaar Sentry</span>`;
            l.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); if(settingsPanel) settingsPanel.style.display='block'; }, true);
            target.appendChild(l);
        }
    };

    // --- DETECTION ENGINE ---
    const highlightItems = () => {
        injectSettingsLink();
        let anyMatch = false;
        const activeBlacklist = ignoredItems.filter(i => i.enabled).map(i => i.name.toLowerCase());
        document.querySelectorAll('[data-testid="item"]').forEach(card => {
            const pDiv = card.querySelector('[data-testid="price"]'), nEl = card.querySelector('[data-testid="name"]');
            const btn = card.querySelector('[data-testid="buy-button"]') || card.querySelector('[data-testid="activate-buy-button"]');
            if (pDiv && nEl && btn) {
                const rEl = pDiv.querySelector('[class*="rates"]'), rTxt = rEl ? rEl.innerText : "";
                const curP = parseInt(pDiv.innerText.replace(rTxt, "").replace(/[^0-9]/g, ''), 10);
                const m = userSettings.find(it => it.enabled && (it.max != null ? (curP >= it.amt && curP <= it.max) : curP === it.amt));
                const isLocked = btn.hasAttribute('disabled') || btn.disabled || btn.classList.contains('disabled');
                if (m && !isLocked && !activeBlacklist.includes(nEl.innerText.trim().toLowerCase())) {
                    card.style.setProperty('background-color', hexToRgba(m.color), 'important');
                    card.style.setProperty('box-shadow', `inset 0 0 0 4px ${m.color}`, 'important');
                    anyMatch = true;
                } else {
                    card.style.removeProperty('background-color');
                    card.style.removeProperty('box-shadow');
                }
            }
        });
        updateFloatingBanner(anyMatch);
    };

    const init = () => {
        createSettingsPanel();
        if (!localStorage.getItem('bbsmalls_sentry_initialized')) {
            if (settingsPanel) settingsPanel.style.display = 'block';
            localStorage.setItem('bbsmalls_sentry_initialized', 'true');
        }
        const observer = new MutationObserver(highlightItems);
        observer.observe(document.body, { childList: true, subtree: true });
        highlightItems();
    };
    init();
})();