hwm_change_class

change class in one click

// ==UserScript==
// @name         hwm_change_class
// @namespace    http://tampermonkey.net/
// @version      2.5.1
// @author       SalmonGOD
// @description  change class in one click
// @match        /^https{0,1}:\/\/((www|qrator|my)\.(heroeswm|lordswm)\.(ru|com)|178\.248\.235\.15)\/(home|castle).php*/
// @include      /^https{0,1}:\/\/((www|qrator|my)\.(heroeswm|lordswm)\.(ru|com)|178\.248\.235\.15)\/(home|castle).php*/
// @license MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let myCode = localStorage.getItem('myCode');
    let currentId = document.cookie.match(/pl_id=\d*/gi).join('');
    let myId = localStorage.getItem('myId');
    let updates_popup_changeClass_toogle = JSON.parse(localStorage.getItem('updates_popup_changeClass_toogle'));

    if (!myId) {
        localStorage.setItem('myId', currentId);
        location.reload();
    }

    if (updates_popup_changeClass_toogle === null) {
        localStorage.setItem('updates_popup_changeClass_toogle', JSON.stringify(true));
        location.reload();
    }

    let links = ['https://my.lordswm.com', 'https://www.heroeswm.ru'];

    let link = location.href.slice(0,22) === 'https://my.lordswm.com' ? links[0] : links[1];

    let fetchCode = async () => {
        fetch(`${link}/castle.php?show_castle_f=1`)
            .then(function(response) {
            return response.text()
        })
            .then(function(html) {
            var parser = new DOMParser();
            var doc = parser.parseFromString(html, "text/html");
            var element = doc.getElementsByClassName('castle_yes_no_buttons')[0].getElementsByTagName('a')[0].href;
            var code = element.slice(-32);
            var plId = document.cookie.match(/pl_id=\d*/gi);
            localStorage.setItem('myCode', code);
        })
            .catch(function(err) {
        });
    }

    if (!myCode) fetchCode();

    if (myId !== currentId) {
        fetchCode();
        localStorage.setItem('myId', currentId);
    }

    let newInterface = document.getElementsByClassName('home_pers_block')[0];

    let oldInterface = [...document.getElementsByTagName('b')];
    oldInterface = oldInterface.filter(el => el.textContent === 'Ваш Персонаж')[0];

    var parentNewInterface = document.getElementsByClassName('home_container_block')[4];

    let block = document.createElement('div');
    block.style = 'display: flex; justify-content: center;'

    if (oldInterface) {
        if (location.href === `${link}/home.php`) {
            let charBtn = [...document.getElementsByTagName('b')];
            charBtn = charBtn.filter(el => el.innerText == 'Ваш Персонаж')[0];
            charBtn.style.cursor = 'pointer';
            charBtn.title = 'Нажмите на кнопку "Персонаж" для повторной загрузки своего уникального кода (в случае смены ника)'
            charBtn.addEventListener('click', () => fetchCode())
            oldInterface.appendChild(block);
        }
    }
    if (newInterface) {
        if (location.href === `${link}/home.php`) {
            let charBtn = [...document.getElementsByClassName('global_container_block_header')];
            charBtn = charBtn.filter(el => el.innerText === 'Персонаж')[0];
            charBtn.style.cursor = 'pointer';
            charBtn.title = 'Нажмите на кнопку "Персонаж" для повторной загрузки своего уникального кода (в случае смены ника)'
            charBtn.addEventListener('click', () => fetchCode())
            newInterface.insertBefore(block, newInterface.firstChild);
        }
    }

    const classes = [
        {name: 'knight', imgHref: 'https://cfcdn.lordswm.com/i/f/r1.png?v=1.1', href: `${link}/castle.php?change_clr_to=1&sign=${myCode}`},
        {name: 'light_knight', imgHref: 'https://cfcdn.lordswm.com/i/f/r101.png?v=1.1', href: `${link}/castle.php?change_clr_to=101&sign=${myCode}`},
        {name: 'necr', imgHref: 'https://cfcdn.lordswm.com/i/f/r2.png?v=1.1', href: `${link}/castle.php?change_clr_to=2&sign=${myCode}`},
        {name: 'nps', imgHref: 'https://cfcdn.lordswm.com/i/f/r102.png?v=1.1', href: `${link}/castle.php?change_clr_to=102&sign=${myCode}`},
        {name: 'mage', imgHref: 'https://cfcdn.lordswm.com/i/f/r3.png?v=1.1', href: `${link}/castle.php?change_clr_to=3&sign=${myCode}`},
        {name: 'mage_destroyer', imgHref: 'https://cfcdn.lordswm.com/i/f/r103.png?v=1.1', href: `${link}/castle.php?change_clr_to=103&sign=${myCode}`},
        {name: 'elf', imgHref: 'https://cfcdn.lordswm.com/i/f/r4.png?v=1.1', href: `${link}/castle.php?change_clr_to=4&sign=${myCode}`},
        {name: 'blue_elf', imgHref: 'https://cfcdn.lordswm.com/i/f/r104.png?v=1.1', href: `${link}/castle.php?change_clr_to=104&sign=${myCode}`},
        {name: 'barb', imgHref: 'https://cfcdn.lordswm.com/i/f/r5.png?v=1.1', href: `${link}/castle.php?change_clr_to=5&sign=${myCode}`},
        {name: 'blood_barb', imgHref: 'https://cfcdn.lordswm.com/i/f/r105.png?v=1.1', href: `${link}/castle.php?change_clr_to=105&sign=${myCode}`},
        {name: 'witch_barb', imgHref: 'https://cfcdn.lordswm.com/i/f/r205.png?v=1.1', href: `${link}/castle.php?change_clr_to=205&sign=${myCode}`},
        {name: 'dark_elf', imgHref: 'https://cfcdn.lordswm.com/i/f/r6.png?v=1.1', href: `${link}/castle.php?change_clr_to=6&sign=${myCode}`},
        {name: 'dark_elf_tamer', imgHref: 'https://cfcdn.lordswm.com/i/f/r106.png?v=1.1', href: `${link}/castle.php?change_clr_to=106&sign=${myCode}`},
        {name: 'demon', imgHref: 'https://cfcdn.lordswm.com/i/f/r7.png?v=1.1', href: `${link}/castle.php?change_clr_to=7&sign=${myCode}`},
        {name: 'dark_demon', imgHref: 'https://cfcdn.lordswm.com/i/f/r107.png?v=1.1', href: `${link}/castle.php?change_clr_to=107&sign=${myCode}`},
        {name: 'dwarf', imgHref: 'https://cfcdn.lordswm.com/i/f/r8.png?v=1.1', href: `${link}/castle.php?change_clr_to=8&sign=${myCode}`},
        {name: 'flame_dwarf', imgHref: 'https://cfcdn.lordswm.com/i/f/r108.png?v=1.1', href: `${link}/castle.php?change_clr_to=108&sign=${myCode}`},
        {name: 'steppe_barb', imgHref: 'https://cfcdn.lordswm.com/i/f/r9.png?v=1.1', href: `${link}/castle.php?change_clr_to=9&sign=${myCode}`},
        {name: 'rage_steppe_barb', imgHref: 'https://cfcdn.lordswm.com/i/f/r109.png?v=1.1', href: `${link}/castle.php?change_clr_to=109&sign=${myCode}`},
        {name: 'pharaon', imgHref: 'https://cfcdn.lordswm.com/i/f/r10.png?v=1.1', href: `${link}/castle.php?change_clr_to=10&sign=${myCode}`},

    ];
    classes.forEach(el => {
        let link = document.createElement('div');
        link.innerHTML = `<div title=${el.name}><img style='width: 19px; height: 19px; cursor: pointer;' src=${el.imgHref} alt=${el.name}/></div>`;
        link.addEventListener('click', () => {
            let fetchChange = async() => {
                fetch(el.href).then(response => {
                    if (response.status === 200) {
                        location.reload();
                    }
                });
            }
            fetchChange();

        })
        block.appendChild(link);
    });

    const updates_popup = document.createElement('div');
    updates_popup.innerText = 'Изминения в скрипте "смена классов":';
    updates_popup.style = 'position: absolute; top: 125px; left: 15px; width: 200px; height: 200px; border: 1px solid black; background: white; padding: 10px; border-radius: 4px;';
    if (updates_popup_changeClass_toogle === true) {
        console.log('here1')
        updates_popup.style.display = 'block'
    } else if (updates_popup_changeClass_toogle === false) {
        console.log('here2')
        updates_popup.style.display = 'none'
    }
    const updates_text = document.createElement('div');
    updates_text.innerText = '1. Добавлена возможность обновить свой "уникальный" код, в случае смены ника нажав на текст "Персонаж" для нового интерфейса или "Ваш персонаж" для старого интерфейса';
    updates_text.style = 'font-weight: bold; border-top: 1px solid black';
    const close_popup_btn = document.createElement('div');
    close_popup_btn.innerText = 'Ознакомился';
    close_popup_btn.style = 'background: #00ff99; width: 110px; height: 20px; text-align: center; border-radius: 7px; cursor: pointer; font-weight: bold; margin-top: 30px; margin-left: 90px;';
    close_popup_btn.addEventListener('click', () => {
        updates_popup.style.display = 'none';
        localStorage.setItem('updates_popup_changeClass_toogle', false);
    })
    updates_popup.append(updates_text);
    updates_popup.append(close_popup_btn);
    document.body.append(updates_popup);
})();