LeG Army filters

Add functional filters in the leader_army page

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         LeG Army filters
// @namespace    https://greasyfork.org/users/722588
// @version      1.8
// @description  Add functional filters in the leader_army page
// @author       Prozyk
// @match        https://www.lordswm.com/leader_army*
// @match        https://www.heroeswm.ru/leader_army*
// @grant        none
// ==/UserScript==


// 1-10: Current factions
// -1: All
// 0: Other
const customBookmarks = {
    '11': { label: 'Lifeless', skills: ['Elemental', 'Undead'] },
    '12': { label: 'Penetrate', skills: ['Ignore defense', 'Lizard charge', 'Poison', 'Venom', 'Harvest Soul'] },
    '13': { label: 'Utility', skills: ['Caster', 'Mirth', 'Confer Luck', 'Aura of bravery', 'Incentive Presence', 'Shield Allies', 'Mana'] },
    '14': { label: 'Shooter', skills: ['Shooter'] }, 
    '15': { label: 'Fast', speed: 8}, // Speed >= 8
    '16': { label: 'Infantry', creatures: ['Warlords', 'Orc warriors', 'Dreadlords', 'Valkyries', 'Pikemen', 'Templars', 'Enforcers', 'Sailor-strangers']}
};

(function() {
    'use strict';
        
    function prettyBookmarks() {
        const bookmarkContainer = document.querySelector('.leader_bookmarks');
        bookmarkContainer.style.width='90px'
        Array.from(bookmarkContainer.children).forEach(child => {
            child.style.width='90px'
        });        
    }
    
    // Add new bookmarks for custom filters
    function addNewBookmarks() {
        const bookmarkContainer = document.querySelector('.leader_bookmarks');
        
        if (!bookmarkContainer) {
            console.error('Bookmark container not found');
            return;
        }

    // Add each bookmark dynamically
    Object.entries(customBookmarks).forEach(function([key, filter]) {
        const newDiv = document.createElement('div');
        newDiv.className = 'bookmark';
        newDiv.id = "bookmark" + key;
        newDiv.bookmark = key; // Bookmark ID  
    
        const span = document.createElement('span');
        span.innerText = filter.label;

        newDiv.appendChild(span);
        bookmarkContainer.appendChild(newDiv);

        // Attach the event listener for filtering
        newDiv.addEventListener('mouseup', applyFilter);
    });

        console.log('New bookmarks added');
    }

    // Function to apply a filter based on skills
    function applyFilter(event) {
        document.getElementById('bookmark'+current_bookmark).classList.add('bookmark');
        document.getElementById('bookmark'+current_bookmark).classList.remove('selected_bookmark');
        current_bookmark = this.bookmark
        document.getElementById('bookmark'+this.bookmark).classList.add('selected_bookmark');
        document.getElementById('bookmark'+this.bookmark).classList.remove('bookmark');
               
        let bookmark = customBookmarks[current_bookmark]
        console.log(bookmark)
        
        let check = `if ((current_bookmark == -1)`;
        
        if ('skills' in bookmark) {
            bookmark.skills.forEach((skill) => {
                skill = skill.toLowerCase()
                check = check + ` || (obj[i]['skills'] && obj[i]['skills'].some(skillItem => skillItem.toLowerCase().includes('${skill}')))`;
            });
        }
                
        
        if ('speed' in bookmark) {
            console.log(bookmark.speed)
            speed = bookmark.speed
            check = check + ` || (obj[i]['skills'] && obj[i]['speed'] >= ${speed})`;
        }
        
        if ('creatures' in bookmark) {
            bookmark.creatures.forEach((creature) => {
                creature = creature.toLowerCase()
                check = check + ` || (obj[i]['skills'] && obj[i]['name'].toLowerCase().includes('${creature}'))`;
            });
        }
        
        check = check + `)`           
//         console.log(check)
        
        var showArmyString = show_army.toString();
//         // Replace the existing race-based filter with a skill-based filter
        showArmyString = showArmyString.replace(
            `if ((current_bookmark == -1)||(obj[i]['race'] == current_bookmark))`,
            check
        );
        let show_army_new = new Function('skip_update', showArmyString.slice(showArmyString.indexOf('{') + 1, -1));
        show_army_new(false);
    }


    addNewBookmarks();
    prettyBookmarks();
})();