Skip to content

Instantly share code, notes, and snippets.

@sorja
Created May 25, 2018 13:50
Show Gist options
  • Save sorja/549ccd82e4cca443352ebbb7ea5d0800 to your computer and use it in GitHub Desktop.
Save sorja/549ccd82e4cca443352ebbb7ea5d0800 to your computer and use it in GitHub Desktop.
alt + x shortcut for google translate swap
// Not following best practices
(function () {
const d = document.getElementById,
a = window.addEventListener,
gt_swap = d('gt-swap'),
source = d('source');
a('keydown', function (e) {
if (e.altKey && e.code === "KeyX") {
source.disabled = true;
gt_swap.dispatchEvent(new MouseEvent('mouseover'));
gt_swap.dispatchEvent(new MouseEvent('mousedown'));
gt_swap.dispatchEvent(new MouseEvent('mouseup'));
}
});
a('keyup', (e) => {
if (e.altKey && e.code === "KeyX") {
//reset disabled textfield and refocus
source.disabled = false;
source.focus();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment