Google Translate Keyboard Shortcut

Press ctrl-s to swap active languages

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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.

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

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         Google Translate Keyboard Shortcut
// @namespace    https://github.com/Greenek
// @version      1.1
// @description  Press ctrl-s to swap active languages
// @author       Paweł Golonko
// @license      MIT; https://opensource.org/licenses/MIT
// @copyright    2017, Paweł Golonko (http://greenek.com)
// @homepageURL  https://github.com/Greenek/google-translate-keyboard-shortcut-userscript
// @supportURL   https://github.com/Greenek/google-translate-keyboard-shortcut-userscript/issues
// @icon         https://raw.githubusercontent.com/Greenek/google-translate-keyboard-shortcut-userscript/master/icon.png
// @grant        none
// @run-at       document-end
// @include      http://translate.google.tld/*
// @include      https://translate.google.tld/*
// ==/UserScript==
(() => {
  'use strict';
  
  // Get swap button element
  const swapBtn = document.getElementById('gt-swap');

  /**
   * Swap languages by emulating mouse click events on UI button.
   */
  function swapLanguages() {
    ['mouseover', 'mousedown', 'mouseup'].forEach(type =>
      swapBtn.dispatchEvent(new MouseEvent(type))
    );
  }

  /**
   * Set listeners for shortcut event.
   */
  window.addEventListener(
    'keypress',
    event => {
      if (event.key === 's' && event.ctrlKey) {
        swapLanguages();
      }
    },
    true
  );
})();