Torn Item Market Highlighter

Highlight items in the item market/bazaars that are at or below Arson Warehouse Pricelist

目前为 2024-10-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         Torn Item Market Highlighter
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Highlight items in the item market/bazaars that are at or below Arson Warehouse Pricelist
// @author       You
// @match        https://www.torn.com/page.php?sid=ItemMarket*
// @match        https://www.torn.com/bazaar.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_addStyle
// @grant         GM_registerMenuCommand
// @grant         GM.xmlHttpRequest
// ==/UserScript==

(function() {
   'use strict';

   GM_addStyle(`
       .price-indicator {
           display: inline-block;
           padding: 2px 4px;
           margin-left: 5px;
           border-radius: 3px;
           font-size: 11px;
           font-weight: bold;
           white-space: nowrap;
       }
       .diff-90-100 { background: #004d00; color: white; }
       .diff-60-90 { background: #006700; color: white; }
       .diff-30-60 { background: #008100; color: white; }
       .diff-0-30 { background: #009b00; color: white; }
       .diff0-30 { background: #cc0000; color: white; }
       .diff30-60 { background: #b30000; color: white; }
       .diff60-90 { background: #990000; color: white; }
       .diff90-plus { background: #800000; color: white; }
       .diff-equal { background: #666666; color: white; }
   `);

   let item_prices = {};

   try {
       item_prices = JSON.parse(GM_getValue("AWH_Prices", "{}"));
   } catch (e) {}

   GM_registerMenuCommand('Add Torn Player ID', () => {
       let torn_id = GM_getValue("AWH_TornID", "");
       torn_id = prompt("Enter your Torn Player ID", torn_id);
       if (torn_id) GM_setValue("AWH_TornID", torn_id);
   });

   GM_registerMenuCommand('Add AWH API key', () => {
       let AWH_Key = GM_getValue("AWH_Key", "");
       AWH_Key = prompt("Enter your AWH API key", AWH_Key);
       if (AWH_Key) GM_setValue("AWH_Key", AWH_Key);
   });

   GM_registerMenuCommand('Get AWH Prices', getAWHPrices);

   function getAWHPrices() {
       const AWH_Key = GM_getValue("AWH_Key", "");
       const torn_id = GM_getValue("AWH_TornID", "");

       if (AWH_Key && torn_id) {
           item_prices = {};
           GM.xmlHttpRequest({
               method: "GET",
               url: `https://arsonwarehouse.com/api/v1/bids/${torn_id}`,
               headers: {
                   "Authorization": "Basic " + btoa(AWH_Key + ':')
               },
               onload: function(response) {
                   try {
                       const items = JSON.parse(response.responseText);
                       if (items.bids?.length > 0) {
                           items.bids.forEach(bid => {
                               if (bid.item_id && bid.bids?.length > 0) {
                                   item_prices[bid.item_id] = bid.bids[0].price || 0;
                               }
                           });
                       }
                       GM_setValue("AWH_Prices", JSON.stringify(item_prices));
                       alert('Prices updated.');
                       processElements();
                   } catch (e) {
                       alert('Error updating prices.');
                   }
               }
           });
       }
   }

   function addPriceIndicator(itemId, itemPrice, container) {
       if (!item_prices[itemId] || !container) return;

       const awhPrice = item_prices[itemId];
       if (!awhPrice) return;

       let indicator = container.querySelector('.price-indicator');
       if (!indicator) {
           indicator = document.createElement('span');
           indicator.classList.add('price-indicator');
           container.appendChild(indicator);
       }

       const priceDiff = ((awhPrice - itemPrice) / awhPrice) * 100;
       indicator.textContent = `${priceDiff > 0 ? '-' : '+'}${Math.abs(priceDiff).toFixed(1)}%`;

       indicator.className = 'price-indicator';

       if (priceDiff === 0) indicator.classList.add('diff-equal');
       else if (priceDiff > 0) {
           if (priceDiff >= 90) indicator.classList.add('diff-90-100');
           else if (priceDiff >= 60) indicator.classList.add('diff-60-90');
           else if (priceDiff >= 30) indicator.classList.add('diff-30-60');
           else indicator.classList.add('diff-0-30');
       } else {
           if (priceDiff <= -90) indicator.classList.add('diff90-plus');
           else if (priceDiff <= -60) indicator.classList.add('diff60-90');
           else if (priceDiff <= -30) indicator.classList.add('diff30-60');
           else indicator.classList.add('diff0-30');
       }
   }

   function processElements() {
       if (document.URL.includes('sid=ItemMarket')) {
           document.querySelectorAll('.itemTile___cbw7w').forEach(tile => {
               const img = tile.querySelector('img.torn-item');
               if (!img) return;

               const idMatch = img.src.match(/\/images\/items\/(\d+)\//);
               if (!idMatch) return;

               const itemId = idMatch[1];
               const priceElement = tile.querySelector('.priceAndTotal___eEVS7');

               if (priceElement) {
                   const priceMatch = priceElement.textContent.match(/\$([0-9,]+)/);
                   if (priceMatch) {
                       const itemPrice = parseInt(priceMatch[1].replace(/,/g, ''));
                       addPriceIndicator(itemId, itemPrice, priceElement);
                   }
               }
           });

           document.querySelectorAll('.sellerRow___AI0m6').forEach(row => {
               const img = row.querySelector('.thumbnail___M_h9v img');
               if (!img) return;

               const idMatch = img.src.match(/\/images\/items\/(\d+)\//);
               if (!idMatch) return;

               const itemId = idMatch[1];
               const priceElement = row.querySelector('.price___Uwiv2');

               if (priceElement) {
                   const priceText = priceElement.textContent;
                   const priceMatch = priceText.match(/\$([0-9,]+)/);
                   if (priceMatch) {
                       const itemPrice = parseInt(priceMatch[1].replace(/,/g, ''));
                       addPriceIndicator(itemId, itemPrice, priceElement);
                   }
               }
           });
       }
       else if (document.URL.includes('bazaar.php')) {
           document.querySelectorAll('img[src*="/images/items/"][src*="/large.png"]').forEach(img => {
               if (!img.parentElement?.parentElement?.parentElement) return;

               const idMatch = img.src.match(/\/images\/items\/(\d+)\//);
               if (!idMatch) return;

               const itemId = idMatch[1];
               const container = img.parentElement.parentElement.parentElement;
               const priceElement = container.querySelector('[class*="price_"]');

               if (priceElement) {
                   const priceMatch = priceElement.textContent.match(/\$([0-9,]+)/);
                   if (priceMatch) {
                       const itemPrice = parseInt(priceMatch[1].replace(/,/g, ''));
                       addPriceIndicator(itemId, itemPrice, priceElement);
                   }
               }
           });
       }
   }

   const observer = new MutationObserver(mutations => {
       processElements();
   });

   setTimeout(() => {
       observer.observe(document.body, {
           childList: true,
           subtree: true
       });
       processElements();
   }, 1000);

})();

QingJ © 2025

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