Elective Translator

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);
        }
    });

})();