Google Translate Keyboard Shortcut

Press ctrl-s to swap active languages

  1. // ==UserScript==
  2. // @name Google Translate Keyboard Shortcut
  3. // @namespace https://github.com/Greenek
  4. // @version 1.1
  5. // @description Press ctrl-s to swap active languages
  6. // @author Paweł Golonko
  7. // @license MIT; https://opensource.org/licenses/MIT
  8. // @copyright 2017, Paweł Golonko (http://greenek.com)
  9. // @homepageURL https://github.com/Greenek/google-translate-keyboard-shortcut-userscript
  10. // @supportURL https://github.com/Greenek/google-translate-keyboard-shortcut-userscript/issues
  11. // @icon https://raw.githubusercontent.com/Greenek/google-translate-keyboard-shortcut-userscript/master/icon.png
  12. // @grant none
  13. // @run-at document-end
  14. // @include http://translate.google.tld/*
  15. // @include https://translate.google.tld/*
  16. // ==/UserScript==
  17. (() => {
  18. 'use strict';
  19. // Get swap button element
  20. const swapBtn = document.getElementById('gt-swap');
  21.  
  22. /**
  23. * Swap languages by emulating mouse click events on UI button.
  24. */
  25. function swapLanguages() {
  26. ['mouseover', 'mousedown', 'mouseup'].forEach(type =>
  27. swapBtn.dispatchEvent(new MouseEvent(type))
  28. );
  29. }
  30.  
  31. /**
  32. * Set listeners for shortcut event.
  33. */
  34. window.addEventListener(
  35. 'keypress',
  36. event => {
  37. if (event.key === 's' && event.ctrlKey) {
  38. swapLanguages();
  39. }
  40. },
  41. true
  42. );
  43. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址