pom.moe Updated

Fixes various issues with the site, such as no character images, outdated banners, and 50/50 indicators not working properly

// ==UserScript==
// @name         pom.moe Updated
// @namespace    http://tampermonkey.net/
// @version      2025-04-13
// @description  Fixes various issues with the site, such as no character images, outdated banners, and 50/50 indicators not working properly
// @author       Idy
// @match        https://pom.moe/warp
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const CHARACTER_BANNER_NAME = "Blossom to the Beyond";
    const CHARACTER_BANNER_IMAGE = "https://i.postimg.cc/QxcxVkk2/Blossom-to-the-Beyond-2025-04-09.png";
    const LIGHTCONE_BANNER_IMAGE = "https://i.postimg.cc/Pfg5tFZb/Brilliant-Fixation-2025-04-09.png";

    const standardBanner = [
        "Bailu", "Bronya", "Clara", "Gepard", "Himeko", "Welt", "Yanqing",
        "But the Battle Isn't Over", "In the Name of the World", "Moment of Victory",
        "Night on the Milky Way", "Sleep Like the Dead", "Something Irreplaceable",
        "Time Waits for No One"
    ];

    // Updated for 3.2
    const images = [
        { src: '/images/characters-mini/aventurine.png', newSrc: 'https://i.postimg.cc/PqWHXrB2/aventurine.png' },
        { src: '/images/characters-mini/jade.png', newSrc: 'https://i.postimg.cc/2jGzLw0m/jade.png' },
        { src: '/images/characters-mini/yunli.png', newSrc: 'https://i.postimg.cc/RFs4yC6h/yunli.png' },
        { src: '/images/characters-mini/jiaoqiu.png', newSrc: 'https://i.postimg.cc/SKZcT8rM/jiaoqiu.png' },
        { src: '/images/characters-mini/feixiao.png', newSrc: 'https://i.postimg.cc/nchQJBhr/feixiao.png' },
        { src: '/images/characters-mini/lingsha.png', newSrc: 'https://i.postimg.cc/3wqWFf61/lingsha.png' },
        { src: '/images/characters-mini/rappa.png', newSrc: 'https://i.postimg.cc/P58bDKs9/rappa.png' },
        { src: '/images/characters-mini/sunday.png', newSrc: 'https://i.postimg.cc/0NLCdtQp/sunday.webp' },
        { src: '/images/characters-mini/fugue.png', newSrc: 'https://i.postimg.cc/G3MrqcQy/fugue.webp' },
        { src: '/images/characters-mini/the-herta.png', newSrc: 'https://i.postimg.cc/XXL2drPw/theherta.webp' },
        { src: '/images/characters-mini/aglaea.png', newSrc: 'https://i.postimg.cc/9FywLyB3/aglaea.webp' },
        { src: '/images/characters-mini/tribbie.png', newSrc: 'https://i.postimg.cc/1zVfxcwk/tribbie.webp' },
        { src: '/images/characters-mini/mydei.png', newSrc: 'https://cdn.starrailstation.com/assets/d65549800df726accd4553ad7380abec44f29ce4c7d40fb9f4fc49b7269a8c00.webp' },
        { src: '/images/characters-mini/castorice.png', newSrc: 'https://cdn.starrailstation.com/assets/c5672716925ad2ab406feeb1d8be27f7fb240060d9f1eb6ad328d76c0a903bc0.webp' },
        { src: '/images/lightcones/dance-at-sunset.png', newSrc: 'https://i.postimg.cc/1XdkP3C7/Dance-at-Sunset.webp' },
        { src: '/images/lightcones/those-many-springs.png', newSrc: 'https://i.postimg.cc/44FkpWfv/Those-Many-Springs.webp' },
        { src: '/images/lightcones/i-venture-forth-to-hunt.png', newSrc: 'https://i.postimg.cc/BnHr9sxN/I-Venture-Forth-To-Hunt.webp' },
        { src: '/images/lightcones/scent-alone-stays-true.png', newSrc: 'https://i.postimg.cc/MTKNQQgr/Scent-Alone-Stays-True.webp' },
        { src: '/images/lightcones/ninjutsu-inscription-dazzling-evilbreaker.png', newSrc: 'https://i.postimg.cc/5yDRTkyZ/Ninjutsu-Inscription-Dazzling-Evilbreaker.webp' },
        { src: '/images/lightcones/a-grounded-ascent.png', newSrc: 'https://i.postimg.cc/DybDLFYF/A-Grounded-Ascent.webp' },
        { src: '/images/lightcones/long-road-leads-home.png', newSrc: 'https://i.postimg.cc/ncBxZvxv/Long-Roads-Lead-Home.webp' },
        { src: '/images/lightcones/into-the-unreachable-veil.png', newSrc: 'https://i.postimg.cc/ykNLPVq2/Into-The-Unreachable-Veil.webp' },
        { src: '/images/lightcones/time-woven-into-gold.png', newSrc: 'https://i.postimg.cc/XJZpd5vz/Time-Woven-Into-Gold.webp' },
        { src: '/images/lightcones/if-time-were-a-flower.png', newSrc: 'https://i.postimg.cc/FHB7nndb/If-Time-Were-a-Flower.webp' },
        { src: '/images/lightcones/flame-of-blood-blaze-my-path.png', newSrc: 'https://cdn.starrailstation.com/assets/5bffdcf33abc58f6c3259ae14eb621b2e0980c451fad8dd86267865b2ef1a553.webp' },
        { src: '/images/lightcones/make-farewells-more-beautiful.png', newSrc: 'https://cdn.starrailstation.com/assets/220ea6d5e8342336b6e419410f8697d37cc6fef51f66e313669efc8e68dd46e0.webp' }
    ];

    let isGuaranteed = false;

    function fixCharacterNames(node, selector, regex, replacer) {
        if (node.nodeType === Node.ELEMENT_NODE) {
            node.querySelectorAll(selector).forEach(el => {
                el.textContent = el.textContent.replace(regex, replacer);
            });
        }
    }

    function updateCharacterImages(images) {
        images.forEach(({ src, newSrc }) => {
            const img = document.querySelector(`img[src="${src}"]`);
            if (img) {
                img.src = newSrc;
                img.width = 64;
                img.height = 64;
            }
        });
    }

    function updateBanner(characterBannerName, characterBannerImage, lightconeBannerImage) {
        const bannerText = document.querySelector('p.text-sm.leading-none.text-white\\/80');
        if (bannerText) {
            bannerText.textContent = `Current: ${characterBannerName}`;
        }

        const characterBannerImageElement = document.querySelector('img.image.border-right.absolute.z-0.h-full.w-auto.select-none.object-cover.svelte-1j5mbf4');
        if (characterBannerImageElement) {
            characterBannerImageElement.src = characterBannerImage;
            characterBannerImageElement.style.objectPosition = '80% center top';
        }

        const lightconeBannerImageElement = document.querySelector('img.image.border-right.absolute.z-0.h-full.w-auto.select-none.object-cover.svelte-1j5mbf4[style*="object-position: 70% 8%;"]');
        if (lightconeBannerImageElement) {
            lightconeBannerImageElement.src = lightconeBannerImage;
        }
    }

    function updateCharacterBackgrounds() {
        const bannerElement = document.querySelector('.flex.flex-wrap');
        if (!bannerElement) return;

        const characterElements = bannerElement.querySelectorAll('div.inline-flex');
        let guaranteedState = false;

        characterElements.forEach((element, index) => {
            const characterName = element.querySelector('span.text-base')?.textContent.trim();

            if (!characterName) return;

            if (standardBanner.includes(characterName)) {
                element.style.backgroundColor = 'rgb(31 41 55 / var(--tw-bg-opacity))';
                guaranteedState = true;
            } else {
                if (guaranteedState) {
                    element.style.backgroundColor = 'rgb(31 41 55 / var(--tw-bg-opacity))';
                } else {
                    element.style.backgroundColor = '#fbcd7433';
                }
                guaranteedState = false;
            }

            if (index === 0 && !standardBanner.includes(characterName)) {
                element.style.backgroundColor = '#fbcd7433';
            }
        });
    }


    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length > 0) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {
                        fixCharacterNames(node, '.inline-flex span', /(character|lightcone)\.([a-zA-Z-]+)/g, (_, type, match) => {
                            return match.split('-').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
                        });

                        updateCharacterImages(images);
                        updateBanner(CHARACTER_BANNER_NAME, CHARACTER_BANNER_IMAGE, LIGHTCONE_BANNER_IMAGE);
                        updateCharacterBackgrounds();
                    }
                });
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();