Elective Translator

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
        }
    });

})();