Elective Translator

Use the mouse to translate the text you want into any language.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Elective Translator
// @namespace    greasyfork.org/users/1573407
// @version      1.1
// @description  Use the mouse to translate the text you want into any language.
// @author       Mustafa Hakan
// @license      MIT
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @connect      translate.googleapis.com
// ==/UserScript==

(function() {
    'use strict';

    // --- PASTEL YEŞİL KUTU STİLİ ---
    GM_addStyle(`
        #mh-auto-trans-box {
            position: fixed;
            z-index: 2147483647;
            display: none;
            background: #e8f5e9;
            border: 2px solid #81c784;
            border-radius: 10px;
            padding: 8px 12px;
            max-width: 400px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.15);
            color: #2e7d32;
            font-family: 'Segoe UI', Tahoma, sans-serif;
            font-size: 14px;
            line-height: 1.4;
            word-break: break-word;
            transition: opacity 0.2s ease;
        }
    `);

    const box = document.createElement('div');
    box.id = 'mh-auto-trans-box';
    document.body.appendChild(box);

    let lastSelection = '';
    let hideTimer;

    function hideBox() {
        box.style.display = 'none';
        box.textContent = '';
    }

    function showBox(html, x, y) {
        box.innerHTML = html;
        box.style.display = 'block';
        box.style.left = x + 'px';
        box.style.top = y + 'px';
    }

    document.addEventListener('mouseup', function(e) {
        clearTimeout(hideTimer);
        const sel = window.getSelection().toString().trim();
        if (!sel || sel === lastSelection) {
            // Seçim yok veya aynı metin -> hemen gizleme, belki başka yere tıklamayla gizlenecek
            hideTimer = setTimeout(hideBox, 200);
            return;
        }

        lastSelection = sel;
        const range = window.getSelection().getRangeAt(0);
        const rect = range.getBoundingClientRect();

        // Kutu konumu: seçimin altına, ortalanmış şekilde
        const boxX = Math.min(Math.max(rect.left + rect.width / 2 - 150, 10), window.innerWidth - 420);
        const boxY = rect.bottom + 5;

        showBox('<span style="color:#81c784;">⏳ Çevriliyor...</span>', boxX, boxY);

        GM_xmlhttpRequest({
            method: 'GET',
            url: `https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=tr&dt=t&q=${encodeURIComponent(sel)}`,
            onload: function(resp) {
                if (sel !== window.getSelection().toString().trim()) {
                    // Seçim değişmiş, çeviri gösterme
                    return;
                }
                try {
                    const json = JSON.parse(resp.responseText);
                    const translated = json[0] && json[0][0] && json[0][0][0];
                    if (translated) {
                        showBox(translated, boxX, boxY);
                    } else {
                        showBox('Çeviri alınamadı', boxX, boxY);
                    }
                } catch(e) {
                    showBox('Hata oluştu', boxX, boxY);
                }
            },
            onerror: function() {
                showBox('Bağlantı hatası', boxX, boxY);
            }
        });
    });

    // Seçim dışına tıklayınca veya başka yere basınca gizle
    document.addEventListener('mousedown', function(e) {
        if (!window.getSelection().toString().trim()) {
            hideTimer = setTimeout(hideBox, 150);
        }
    });

})();