JanitorAI Token Filter

Filters character cards on JanitorAI by token count

От 09.04.2025. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

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

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         JanitorAI Token Filter
// @namespace    http://tampermonkey.net/
// @version      0.12
// @description  Filters character cards on JanitorAI by token count
// @author       You
// @match        https://janitorai.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let MIN_TOKENS = 2000;
    let selectElement = null;

    function parseTokens(tokenText) {
        try {
            let cleanText = tokenText.replace(/<!--[\s\S]*?-->/g, '').replace('tokens', '').trim();
            if (cleanText.includes('k')) {
                return parseFloat(cleanText.replace('k', '')) * 1000;
            }
            return parseInt(cleanText, 10) || 0;
        } catch (error) {
            return 0;
        }
    }

    function filterCards() {
        const cards = document.querySelectorAll('.chakra-stack.css-1s5evre');
        cards.forEach(card => {
            const tokenElement = card.querySelector('.chakra-text.css-jccmq6');
            if (!tokenElement) return;

            const tokenCount = parseTokens(tokenElement.textContent);
            const parentContainer = card.closest('.css-1sxhvxh');
            if (parentContainer) {
                parentContainer.style.display = tokenCount < MIN_TOKENS ? 'none' : '';
            }
        });
    }

    function createOrUpdateSelector() {
        if (!selectElement) {
            selectElement = document.createElement('select');
            selectElement.id = 'token-filter-select';
            selectElement.style.position = 'fixed';
            selectElement.style.top = '10px';
            selectElement.style.left = '10px';
            selectElement.style.zIndex = '99999';
            selectElement.style.padding = '4px 8px';
            selectElement.style.backgroundColor = '#4a4a4a';
            selectElement.style.border = '1px solid #666';
            selectElement.style.borderRadius = '4px';
            selectElement.style.fontSize = '12px';
            selectElement.style.color = '#fff';
            selectElement.style.cursor = 'pointer';

            const options = [100, 500, 1000, 1500, 2000, 2500, 3000, 4000];
            options.forEach(value => {
                const option = document.createElement('option');
                option.value = value;
                option.text = `${value} tokens`;
                if (value === MIN_TOKENS) option.selected = true;
                selectElement.appendChild(option);
            });

            selectElement.addEventListener('change', (e) => {
                MIN_TOKENS = parseInt(e.target.value);
                filterCards();
            });

            const appendSelector = () => {
                if (document.body) {
                    document.body.appendChild(selectElement);
                } else {
                    setTimeout(appendSelector, 500);
                }
            };
            appendSelector();
        }

        selectElement.style.display = window.location.pathname === '/' ? 'block' : 'none';
    }

    function initialize() {
        createOrUpdateSelector();
        if (window.location.pathname === '/') {
            filterCards();
        }
    }

    const tryInitialize = () => {
        if (document.body) {
            initialize();

            let lastPath = window.location.pathname;
            const checkPath = () => {
                if (lastPath !== window.location.pathname) {
                    lastPath = window.location.pathname;
                    createOrUpdateSelector();
                    if (lastPath === '/') {
                        filterCards();
                    }
                }
            };

            setInterval(checkPath, 500);

            const observer = new MutationObserver(() => {
                if (window.location.pathname === '/') {
                    setTimeout(filterCards, 500);
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
        } else {
            setTimeout(tryInitialize, 1000);
        }
    };

    tryInitialize();
})();