Energy-based Toggle Clicker with Trigger v4

Clique sur un bouton en fonction du niveau d'énergie lorsqu'il est déclenché par un bouton personnalisé et copie le src de l'iframe dans le presse-papiers. Remplace certaines URL de script sur hamsterkombat.io

Version au 18/06/2024. Voir la dernière version.

// ==UserScript==
// @name         Energy-based Toggle Clicker with Trigger v4
// @namespace    https://hamsterkombat.io/*
// @version      2024-06-18
// @description  Нажимает кнопку на основе уровня энергии при срабатывании пользовательской кнопки и копирует прямую ссылку на hamsterkombat.io в буфер обмена. Подменяет скрипты для работы в браузере
// @description:en Clicks a button based on energy level when triggered by a custom button and copies iframe src to clipboard. Replaces certain script URLs on hamsterkombat.io
// @description:ru Нажимает кнопку на основе уровня энергии при срабатывании пользовательской кнопки и копирует src iframe в буфер обмена. Заменяет определенные URL-адреса скриптов на hamsterkombat.io
// @description:es Hace clic en un botón según el nivel de energía cuando se activa con un botón personalizado y copia el src del iframe al portapapeles. Reemplaza ciertas URL de scripts en hamsterkombat.io
// @description:de Klickt auf eine Schaltfläche basierend auf dem Energieniveau, wenn sie durch eine benutzerdefinierte Schaltfläche ausgelöst wird, und kopiert die iframe-src in die Zwischenablage. Ersetzt bestimmte Skript-URLs auf hamsterkombat.io
// @description:fr Clique sur un bouton en fonction du niveau d'énergie lorsqu'il est déclenché par un bouton personnalisé et copie le src de l'iframe dans le presse-papiers. Remplace certaines URL de script sur hamsterkombat.io
// @description:zh 根据能量水平点击一个按钮,当由自定义按钮触发时,并将iframe的src复制到剪贴板。替换hamsterkombat.io上的某些脚本URL
// @author       Devitp001
// @match        https://*.hamsterkombat.io/*
// @match        https://web.telegram.org/*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=telegram.org
// @grant        none
// @license      MIT
// ==/UserScript==

'use strict';

(function() {
    const currentUrl = window.location.href;

    // Function to replace script URLs
    function replaceScriptUrl() {
        const urlsToReplace = [
            'https://hamsterkombat.io/js/telegram-web-app.js',
            'https://app.hamsterkombat.io/js/telegram-web-app.js'
        ];
        const newUrl = 'https://ktnff.tech/hamsterkombat/telegram-web-app.js';

        const scripts = document.querySelectorAll('script');
        scripts.forEach(script => {
            if (urlsToReplace.includes(script.src)) {
                const newScript = document.createElement('script');
                newScript.src = newUrl;
                newScript.type = 'text/javascript';
                script.parentNode.replaceChild(newScript, script);
                console.log('Script URL replaced:', newScript.src);
            }
        });
    }

    // Observer for script replacement
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length) {
                replaceScriptUrl();
            }
        });
    });

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

    // Functionality for hamsterkombat.io
    function hamsterkombatFunctionality() {
        const { innerWidth, innerHeight } = window;

        const centerX = innerWidth / 2;
        const centerY = innerHeight / 2;

        const dispersionRadius = innerHeight * 0.15; // 10% of window height

        function getRandomOffset() {
            return (Math.random() - 0.5) * 2 * dispersionRadius;
        }

        function createPointerEvents() {
            const clientX = centerX + getRandomOffset();
            const clientY = centerY + getRandomOffset();
            return {
                down: new PointerEvent('pointerdown', { clientX, clientY }),
                up: new PointerEvent('pointerup', { clientX, clientY })
            };
        }

        let isClicking = false;
        const energyThreshold = 100;
        const buttonStyles = {
            position: 'fixed',
            top: '10px',
            left: '10px',
            zIndex: '1000',
            padding: '10px 20px',
            color: 'white',
            border: 'none',
            borderRadius: '5px',
            cursor: 'pointer',
            margin: '5px'
        };

        function clickButton(tapButton, events) {
            tapButton.dispatchEvent(events.down);
            tapButton.dispatchEvent(events.up);
        }

        function checkEnergyAndClick() {
            if (!isClicking) return;

            const energyElement = document.querySelector(".user-tap-energy p");
            if (energyElement) {
                const energy = parseInt(energyElement.textContent.split(" / ")[0], 10);
                if (energy > energyThreshold) {
                    const tapButton = document.querySelector('.user-tap-button');
                    if (tapButton) {
                        const events = createPointerEvents();
                        clickButton(tapButton, events);
                    }
                }
            }

            requestAnimationFrame(checkEnergyAndClick);
        }

        function toggleClicking() {
            isClicking = !isClicking;
            updateClickButtonState(document.getElementById('clickButton'));
            if (isClicking) {
                requestAnimationFrame(checkEnergyAndClick);
            }
        }

        function updateClickButtonState(button) {
            if (isClicking) {
                button.style.backgroundColor = '#4CAF50'; // green
                button.textContent = 'Остановить клики';
            } else {
                button.style.backgroundColor = '#FF0000'; // red
                button.textContent = 'Начать кликать';
            }
        }

        function createClickButton() {
            let clickButton = document.getElementById('clickButton');
            if (!clickButton) {
                clickButton = document.createElement('button');
                clickButton.id = 'clickButton';
                Object.assign(clickButton.style, buttonStyles);
                updateClickButtonState(clickButton);

                clickButton.addEventListener('click', toggleClicking);

                document.body.appendChild(clickButton);
            }
        }

        window.addEventListener('load', () => {
            createClickButton();

            const observer = new MutationObserver(() => {
                if (!document.getElementById('clickButton')) {
                    createClickButton();
                }
            });

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

    // Functionality for web.telegram.org
    function telegramFunctionality() {
        function waitForIframe(selector, callback) {
            const iframe = document.querySelector(selector);
            if (iframe) {
                callback(iframe);
            } else {
                const observer = new MutationObserver((mutations) => {
                    mutations.forEach((mutation) => {
                        if (mutation.addedNodes.length) {
                            const iframe = document.querySelector(selector);
                            if (iframe) {
                                observer.disconnect();
                                callback(iframe);
                            }
                        }
                    });
                });

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

        function getIframeSrc(callback) {
            const selector = "body > div.popup.popup-payment.popup-payment-verification.popup-web-app.active > div > div.popup-body > iframe";
            waitForIframe(selector, (iframe) => {
                const src = iframe.getAttribute('src');
                if (callback) callback(src);
            });
        }

        function copyToClipboard(text) {
            navigator.clipboard.writeText(text).then(() => {
                console.log('Link copied to clipboard!');
            }).catch(err => {
                console.error('Error copying to clipboard: ', err);
            });
        }

        function createCopyButton() {
            const copyButtonStyles = {
                position: 'fixed',
                top: '10px',
                right: '10px',
                zIndex: '1000',
                padding: '10px 20px',
                backgroundColor: '#4CAF50',
                color: 'white',
                border: 'none',
                borderRadius: '5px',
                cursor: 'pointer',
                margin: '5px'
            };

            let copyButton = document.getElementById('copyButton');
            if (!copyButton) {
                copyButton = document.createElement('button');
                copyButton.id = 'copyButton';
                copyButton.textContent = 'Копировать ссылку';
                Object.assign(copyButton.style, copyButtonStyles);

                copyButton.addEventListener('click', () => {
                    getIframeSrc((src) => {
                        if (src) {
                            copyToClipboard(src);
                        } else {
                            console.error('Link not found.');
                        }
                    });
                });

                document.body.appendChild(copyButton);
            }
        }

        window.addEventListener('load', () => {
            createCopyButton();

            const observer = new MutationObserver(() => {
                if (!document.getElementById('copyButton')) {
                    createCopyButton();
                }
            });

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

    if (currentUrl.includes('web.telegram.org')) {
        telegramFunctionality();
    } else if (currentUrl.includes('hamsterkombat.io')) {
        hamsterkombatFunctionality();
    }
})();
长期地址
遇到问题?请前往 GitHub 提 Issues。