Castle Clash Code Selector (Multilingual)

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

目前為 2025-05-09 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Castle Clash Code Selector (Multilingual)
// @name:en      Castle Clash Code Selector (Multilingual)
// @name:ru      Выбор промокода Castle Clash (Многоязычный)
// @namespace    http://tampermonkey.net/
// @version      1.7.2
// @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 urlParams = new URLSearchParams(window.location.search);
    const lang = urlParams.get('lang') || 'en'; // По умолчанию — английский

    // === Переводы ===
    const translations = {
        en: {
            title: "Select a promo code:",
            btnText: "Promo Code"
        },
        ru: {
            title: "Выберите промокод:",
            btnText: "Промокод"
        }
    };

    const currentLang = translations[lang] ? lang : 'en';
    const t = translations[currentLang];

    // === Промокоды ===
    const codes = {
        "1": "QYFUF6",
        "2": "NYQZKQ",
        "3": "KJYJT3",
        "4": "DN72M7",
        "5": "GDEJ9E"
    };

    // === Стили ===
    const style = document.createElement('style');
    style.textContent = `
        #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;
        }
        #code-selector h3 {
            margin-top: 0;
        }
        .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;
        }
        .code-btn:hover {
            background-color: #0056b3;
        }
    `;
    document.head.appendChild(style);

    // === Создание UI ===
    function createCodeSelectorUI() {
        const container = document.createElement('div');
        container.id = 'code-selector';
        container.innerHTML = `<h3>${t.title}</h3>`;

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

        document.body.appendChild(container);
    }

    // === Вставка кода в поле ввода ===
    function insertCode(code) {
        const input = document.querySelector("#cdkey");
        if (input) {
            input.value = code;
            triggerInputEvent(input);
        } else {
            waitForInputElementAndInsert(code);
        }
    }

    // === Эмуляция события ввода ===
    function triggerInputEvent(inputElement) {
        const event = new Event('input', { bubbles: true });
        inputElement.dispatchEvent(event);
    }

    // === Ожидание поля ввода через MutationObserver ===
    function waitForInputElementAndInsert(code) {
        const observer = new MutationObserver(() => {
            const input = document.querySelector("#cdkey");
            if (input) {
                input.value = code;
                triggerInputEvent(input);
                observer.disconnect();
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

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

})();