Elective Translator

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

})();