rewrite keybindings

This commit is contained in:
jkhsjdhjs 2017-12-02 21:21:46 +01:00
parent cbf41569dc
commit bc991ca798

View File

@ -1,27 +1,22 @@
function arrowKeys(e) {
switch(e.keyCode) {
case 39:
var prev = document.getElementById('prev');
if(prev) prev.click();
break;
case 37:
var next = document.getElementById('next');
if(next) next.click();
break;
case 82:
var random = document.getElementById('random');
if(random) random.click();
const keybindings = {
'ArrowLeft': clickOnElementBinding('#next'),
'ArrowRight': clickOnElementBinding('#prev'),
'r': clickOnElementBinding('#random')
};
function clickOnElementBinding(selector) {
return () => {
let elem = document.querySelector(selector);
if(elem)
elem.click();
};
}
(function() {
document.addEventListener('keydown', e => {
if(e.key in keybindings) {
e.preventDefault();
keybindings[e.key]();
}
}
if(document.readyState == 'complete' || document.readyState == 'loaded')
document.addEventListener('keydown', arrowKeys);
else {
if(/Chrome/i.test(navigator.userAgent))
window.addEventListener('load', function () {
document.addEventListener('keydown', arrowKeys);
});
else
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('keydown', arrowKeys);
});
}
})();