Google Translate - Switch Languages Hotkey

Makes tab a hotkey to swap languages.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Google Translate - Switch Languages Hotkey
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Makes tab a hotkey to swap languages.
// @author       Meztihn
// @match        http*://translate.google.com/*
// @match        http*://translate.google.co.uk/*
// @match        http*://translate.google.ru/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const tabKeyCode = 'Tab';
    const sourceTextArea = document.getElementById('source')
    const swapLanguagesButton = document.getElementById('gt-swap');

    sourceTextArea.addEventListener('keydown', onKeyDown, { capture: true });

    function onKeyDown(event) {
        if (event.code === tabKeyCode) {
            swapLanguages();
            event.preventDefault(); // prevents active element change and thus focus lose
        }
    }

    function swapLanguages() {
        click(swapLanguagesButton);

        // Standard click() doesn't work
        function click(button) {
            button.dispatchEvent(new MouseEvent('mouseover'));
            button.dispatchEvent(new MouseEvent('mousedown'));
            button.dispatchEvent(new MouseEvent('mouseup'));
        }
    }
})();