Elective Translator

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

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==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);
        }
    });

})();