|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +(async () => { |
| 4 | + await featureManagerLoaded(); |
| 5 | + |
| 6 | + const CLASS_NAME = "tt-sub-vendor-highlight"; |
| 7 | + let observer; |
| 8 | + |
| 9 | + // noinspection JSIncompatibleTypesComparison |
| 10 | + featureManager.registerFeature( |
| 11 | + "Highlight Cheap Items", |
| 12 | + "bazaar", |
| 13 | + () => settings.pages.bazaar.highlightSubVendorItems !== "", |
| 14 | + initialise, |
| 15 | + highlightEverything, |
| 16 | + removeHighlights, |
| 17 | + { |
| 18 | + storage: ["settings.pages.bazaar.highlightSubVendorItems"], |
| 19 | + }, |
| 20 | + () => { |
| 21 | + if (!hasAPIData()) return "No API access."; |
| 22 | + } |
| 23 | + ); |
| 24 | + |
| 25 | + async function initialise() { |
| 26 | + observer = new MutationObserver(() => { highlightEverything() }); |
| 27 | + observer.observe(document.body, { |
| 28 | + childList: true, |
| 29 | + subtree: true |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + function highlightEverything() { |
| 34 | + const items = [...document.findAll("[class*='item__'] > [class*='itemDescription__']")] |
| 35 | + .map((element) => { |
| 36 | + return { |
| 37 | + element, |
| 38 | + id: element.find("img").src.getNumber(), |
| 39 | + price: element.find("[class*='price___']").textContent.getNumber() |
| 40 | + } |
| 41 | + }) |
| 42 | + .filter((item) => item.element); |
| 43 | + |
| 44 | + items.forEach((item) => handleItem(item)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Should highlight the given item based on the price? |
| 49 | + * @param id {number|string} |
| 50 | + * @param price {number} |
| 51 | + * @returns {boolean} |
| 52 | + */ |
| 53 | + function shouldHighlight(id, price) { |
| 54 | + return price < torndata.items[id]?.sell_price; |
| 55 | + } |
| 56 | + |
| 57 | + function handleItem(item) { |
| 58 | + if (shouldHighlight(item.id, item.price)) { |
| 59 | + item.element.parentElement.classList.add(CLASS_NAME); |
| 60 | + } else { |
| 61 | + item.element.parentElement.classList.remove(CLASS_NAME); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function removeHighlights() { |
| 66 | + observer?.disconnect(); |
| 67 | + document.findAll(`.${CLASS_NAME}`) |
| 68 | + .forEach((item) => item.classList.remove(CLASS_NAME)); |
| 69 | + } |
| 70 | +})(); |
0 commit comments