Castle Clash Code Selector (Multilingual & Optimized)

Multilingual promo-code selector and language switcher for Castle Clash

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

您需要先安裝使用者腳本管理器擴展,如 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 & Optimized)
// @name:en      Castle Clash Code Selector (Multilingual & Optimized)
// @name:ru      Выбор промокода Castle Clash (многоязычный и оптимизированный)
// @namespace    http://tampermonkey.net/
// @version      1.8.4
// @description  Multilingual promo-code selector and language switcher for Castle Clash
// @description:en Multilingual promo-code selector and language switcher for Castle Clash
// @description:ru Многоязычный выбор промокодов и переключатель языка для Castle Clash
// @match        https://castleclash.igg.com/event/cdkey*
// @grant        none
// @license      MIT
// ==/UserScript==


(function() {
    'use strict';

    // --- Configuration ---
    const CODES = [
        { id: '1', code: 'QYFUF6' },
        { id: '2', code: 'NYQZKQ' },
        { id: '3', code: 'KJYJT3' },
        { id: '4', code: 'DN72M7' },
        { id: '5', code: 'GDEJ9E' }
    ];

    const TRANSLATIONS = {
        en: { title: 'Select a promo code:', btnPrefix: '', switchLabel: 'Language:' },
        ru: { title: 'Выберите промокод:', btnPrefix: '', switchLabel: 'Язык:' }
    };

    // --- Helpers ---
    const params = new URLSearchParams(location.search);
    const currentLang = params.get('lang') in TRANSLATIONS ? params.get('lang') : 'en';
    const t = TRANSLATIONS[currentLang];

    const basePath = location.origin + '/event/cdkey';
    function langUrl(lang) {
        return `${basePath}?lang=${lang}`;
    }

    // --- Styles ---
    const css = `
        #cc-selector { position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
                       background: #fff; border: 1px solid #ccc; padding: 15px; z-index:9999;
                       box-shadow:0 4px 10px rgba(0,0,0,0.2); border-radius:8px; font-family:sans-serif; }
        #cc-selector h3 { margin:0 0 10px; font-size:16px; }
        .cc-btn { margin:4px; padding:8px 12px; background:#007bff; color:#fff;
                   border:none; border-radius:4px; cursor:pointer; font-size:14px; }
        .cc-btn:hover { background:#0056b3; }
        #cc-language { margin-bottom:8px; display:flex; align-items:center; gap:6px; }
        #cc-language a { text-decoration:none; font-size:14px; color:#007bff; }
        #cc-language a.active { font-weight:bold; cursor:default; }
    `;
    const styleNode = document.createElement('style');
    styleNode.textContent = css;
    document.head.append(styleNode);

    // --- UI Creation ---
    function buildUI() {
        const container = document.createElement('div');
        container.id = 'cc-selector';

        // Language Switcher
        const langDiv = document.createElement('div');
        langDiv.id = 'cc-language';
        langDiv.innerHTML = `<span>${t.switchLabel}</span>`;
        Object.keys(TRANSLATIONS).forEach(lang => {
            const a = document.createElement('a');
            a.href = langUrl(lang);
            a.textContent = lang.toUpperCase();
            if (lang === currentLang) a.classList.add('active');
            langDiv.append(a);
        });
        container.append(langDiv);

        // Title
        const titleEl = document.createElement('h3');
        titleEl.textContent = t.title;
        container.append(titleEl);

        // Buttons
        CODES.forEach(({ id, code }) => {
            const btn = document.createElement('button');
            btn.className = 'cc-btn';
            btn.textContent = `${id}. ${code}`;
            btn.addEventListener('click', () => insertCode(code));
            container.append(btn);
        });

        document.body.append(container);
    }

    // --- Insert & Trigger ---
    function insertCode(code) {
        const input = document.querySelector('#cdkey');
        if (input) {
            input.value = code;
            input.dispatchEvent(new Event('input', { bubbles: true }));
        }
    }

    // --- Init ---
    window.addEventListener('load', buildUI);
})();