Castle Clash Code Selector (Multilingual)

Выбор и автоматическая вставка промокода для Castle Clash с поддержкой нескольких языков

当前为 2025-05-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         Castle Clash Code Selector (Multilingual)
// @name:en      Castle Clash Code Selector (Multilingual)
// @name:ru      Выбор промокода Castle Clash (Многоязычный)
// @namespace    http://tampermonkey.net/
// @version      1.8.0
// @description  Выбор и автоматическая вставка промокода для Castle Clash с поддержкой нескольких языков
// @description:en  Select and auto-insert promo code for Castle Clash with multilingual support
// @description:ru  Выберите и автоматически вставьте промокод для Castle Clash с поддержкой нескольких языков
// @match        https://castleclash.igg.com/event/cdkey/ *  
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // Конфигурация
    const CONFIG = {
        supportedLanguages: ['en', 'ru'],
        defaultLanguage: 'en',
        codeSelectorId: 'cc-code-selector',
        promoCodes: {
            "1": "QYFUF6",
            "2": "NYQZKQ",
            "3": "KJYJT3",
            "4": "DN72M7",
            "5": "GDEJ9E"
        },
        styles: `
            #cc-code-selector {
                position: fixed;
                top: 20px;
                left: 50%;
                transform: translateX(-50%);
                background: #fff;
                border: 1px solid #ccc;
                padding: 15px 20px;
                z-index: 99999;
                box-shadow: 0 4px 10px rgba(0,0,0,0.2);
                font-family: sans-serif;
                text-align: center;
                border-radius: 8px;
                max-width: 90vw;
                overflow-x: auto;
            }
            #cc-code-selector h3 {
                margin-top: 0;
                margin-bottom: 10px;
                color: #2c3e50;
            }
            .cc-code-btn {
                display: inline-block;
                margin: 5px;
                padding: 8px 14px;
                background-color: #007bff;
                color: white;
                border: none;
                border-radius: 5px;
                cursor: pointer;
                font-size: 14px;
                transition: background-color 0.3s ease;
            }
            .cc-code-btn:hover {
                background-color: #0056b3;
            }
            .cc-error-message {
                color: #e74c3c;
                margin-top: 10px;
                display: none;
            }
        `
    };

    // Переводы
    const TRANSLATIONS = {
        en: {
            title: "Select a promo code:",
            btnText: "Promo Code",
            error: "Input field not found!"
        },
        ru: {
            title: "Выберите промокод:",
            btnText: "Промокод",
            error: "Поле ввода не найдено!"
        }
    };

    // Инициализация
    class PromoCodeSelector {
        constructor() {
            this.currentLang = this.determineLanguage();
            this.t = TRANSLATIONS[this.currentLang];
            this.codes = CONFIG.promoCodes;
            this.init();
        }

        determineLanguage() {
            const urlParams = new URLSearchParams(window.location.search);
            const lang = urlParams.get('lang')?.toLowerCase();
            
            return CONFIG.supportedLanguages.includes(lang) 
                ? lang 
                : CONFIG.defaultLanguage;
        }

        injectStyles() {
            const style = document.createElement('style');
            style.textContent = CONFIG.styles;
            document.head.appendChild(style);
        }

        createUI() {
            const container = document.createElement('div');
            container.id = CONFIG.codeSelectorId;
            
            container.innerHTML = `
                <h3>${this.t.title}</h3>
                <div id="cc-code-buttons"></div>
                <div class="cc-error-message" id="cc-error-message">${this.t.error}</div>
            `;

            const buttonsContainer = container.querySelector('#cc-code-buttons');
            
            Object.entries(this.codes).forEach(([key, code]) => {
                const btn = document.createElement('button');
                btn.className = 'cc-code-btn';
                btn.textContent = `${key}. ${code}`;
                btn.onclick = () => this.insertCode(code);
                buttonsContainer.appendChild(btn);
            });

            document.body.appendChild(container);
        }

        insertCode(code) {
            const input = document.querySelector("#cdkey");
            
            if (input) {
                input.value = code;
                this.triggerInputEvent(input);
                this.hideError();
            } else {
                this.showError();
                this.waitForInput(code);
            }
        }

        triggerInputEvent(inputElement) {
            const event = new Event('input', { bubbles: true });
            inputElement.dispatchEvent(event);
        }

        showError() {
            const errorEl = document.getElementById('cc-error-message');
            if (errorEl) {
                errorEl.style.display = 'block';
                setTimeout(() => this.hideError(), 5000);
            }
        }

        hideError() {
            const errorEl = document.getElementById('cc-error-message');
            if (errorEl) errorEl.style.display = 'none';
        }

        waitForInput(code) {
            let attempts = 0;
            const maxAttempts = 10;
            const interval = setInterval(() => {
                const input = document.querySelector("#cdkey");
                if (input) {
                    clearInterval(interval);
                    input.value = code;
                    this.triggerInputEvent(input);
                    this.hideError();
                } else if (++attempts >= maxAttempts) {
                    clearInterval(interval);
                }
            }, 500);
        }

        init() {
            this.injectStyles();
            this.createUI();
        }
    }

    // Запуск скрипта после загрузки
    window.addEventListener('load', () => {
        new PromoCodeSelector();
    });

})();