HWM Buttons Mover

Move mirror button from left panel to right panel

// ==UserScript==
// @name           HWM Buttons Mover
// @author         Neleus
// @namespace      Neleus
// @description    Move mirror button from left panel to right panel
// @version        1.0
// @include        https://www.heroeswm.ru/war.php*
// @include        https://mirror.heroeswm.ru/war.php*
// @include        https://lordswm.com/war.php*
// @include        https://my.lordswm.com/war.php*
// @grant          none
// @license        GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';

    function moveFastBattleButtons() {
        // Find the fastbattle elements in the left panel
        const fastBattleOnElement = document.getElementById('fastbattle_on');
        const fastBattleOffElement = document.getElementById('fastbattle_off');
        
        // Find the magicbook_button element in the right panel
        const magicBookElement = document.getElementById('magicbook_button');
        
        // Get the right panel container
        const rightPanel = document.getElementById('right_button');
        
        if (magicBookElement && rightPanel) {
            let moved = false;
            
            // Move fastbattle_on if it exists
            if (fastBattleOnElement) {
                fastBattleOnElement.remove();
                magicBookElement.insertAdjacentElement('afterend', fastBattleOnElement);
                moved = true;
            }
            
            // Move fastbattle_off if it exists
            if (fastBattleOffElement) {
                fastBattleOffElement.remove();
                // Insert after fastbattle_on if it was moved, otherwise after magicbook_button
                const insertAfter = fastBattleOnElement || magicBookElement;
                insertAfter.insertAdjacentElement('afterend', fastBattleOffElement);
                moved = true;
            }
            
            if (moved) {
                console.log('FastBattle buttons moved to right panel successfully');
            }
        }
    }

    // Function to wait for elements to load
    function waitForElements() {
        const checkInterval = setInterval(() => {
            const fastBattleOnElement = document.getElementById('fastbattle_on');
            const fastBattleOffElement = document.getElementById('fastbattle_off');
            const magicBookElement = document.getElementById('magicbook_button');
            
            if ((fastBattleOnElement || fastBattleOffElement) && magicBookElement) {
                clearInterval(checkInterval);
                moveFastBattleButtons();
            }
        }, 100);

        // Stop checking after 10 seconds to prevent infinite loop
        setTimeout(() => {
            clearInterval(checkInterval);
        }, 10000);
    }

    // Run when DOM is loaded
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', waitForElements);
    } else {
        waitForElements();
    }

    // Also run on dynamic content changes (in case the page updates via AJAX)
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList') {
                const fastBattleOnElement = document.getElementById('fastbattle_on');
                const fastBattleOffElement = document.getElementById('fastbattle_off');
                const magicBookElement = document.getElementById('magicbook_button');
                
                // Check if any fastbattle element is back in the left panel
                if ((fastBattleOnElement || fastBattleOffElement) && magicBookElement) {
                    const leftPanel = document.getElementById('left_button');
                    const needsMove = (fastBattleOnElement && leftPanel && leftPanel.contains(fastBattleOnElement)) ||
                                     (fastBattleOffElement && leftPanel && leftPanel.contains(fastBattleOffElement));
                    
                    if (needsMove) {
                        moveFastBattleButtons();
                    }
                }
            }
        });
    });

    // Start observing changes to the document
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

})();