Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save X-Raym/f3808540e410c09a2a2fb2e48677fac0 to your computer and use it in GitHub Desktop.
Save X-Raym/f3808540e410c09a2a2fb2e48677fac0 to your computer and use it in GitHub Desktop.
GreaseMonkey Navigation wih Left and Right keys
// ==UserScript==
// @name Navigation wih Left and Right keys
// @version 1
// @grant none
// @include http://www.example.com/*
// ==/UserScript==
// USER CONFIG AREA ******************* //
var selector_1 = 'body > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > p:nth-child(3) > a:nth-child(1) > img:nth-child(1)';
var selector_2 = 'body > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(1) > p:nth-child(3) > a:nth-child(2) > img:nth-child(1)';
// ************************************ //
function eventFire(el, etype){
if (el.fireEvent) {
el.fireEvent('on' + etype);
} else {
var evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
}
/*--- Get or set the codes for the arrow keys.
Firefox gives us nice constants, Chrome does not.
*/
KeyEvent = (typeof KeyEvent === "object") ? KeyEvent : [];
const LEFT_KEY = KeyEvent.DOM_VK_LEFT || 37;
const RIGHT_KEY = KeyEvent.DOM_VK_RIGHT || 39;
window.addEventListener ("keydown", keyboardHandler, false);
function keyboardHandler (zEvent) {
var bBlockDefaultAction = false;
//--- Assume we want only the plain keys, not the modified versions.
if (zEvent.altKey || zEvent.ctrlKey || zEvent.shiftKey) {
//-- Do nothing (most user-friendly option, in most cases).
}
else {
if (zEvent.which == LEFT_KEY) {
//DO LEFT KEY ACTION HERE.
bBlockDefaultAction = true;
var elm = document.querySelectorAll( selector_1 )
elm[0].click();
}
else if (zEvent.which == RIGHT_KEY) {
//DO RIGHT KEY ACTION HERE.
bBlockDefaultAction = true;
var elm = document.querySelectorAll( selector_2 )
elm[0].click();
}
}
if (bBlockDefaultAction) {
zEvent.preventDefault ();
zEvent.stopPropagation ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment