Created
July 12, 2016 16:18
-
-
Save dangarthwaite/d18ad1d874b1135f0611331abd803bdd to your computer and use it in GitHub Desktop.
Userscript to add j/k keyboard navigation to zerohedge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name ZeroCruft | |
// @version 1.1 | |
// @namespace 1bae28cfed0eddb302b3ac9b578412a3 | |
// @author Dan Garthwaite <[email protected]> | |
// @description Removes the cruft from the new zerohedge | |
// @include http://*zerohedge.com/* | |
// @run-at document-end | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var page = document.getElementById('page'); | |
var sidebarLeft = document.getElementById('sidebar-left'); | |
var sidebarRight = document.getElementById('sidebar-right'); | |
page.style.maxWidth = "80em"; | |
page.style.marginLeft = page.style.marginRight = "auto"; | |
sidebarLeft.remove(); | |
sidebarRight.remove(); | |
var jk = function(a, inc) { | |
if (!a.length) { return; } | |
for (var i = 0; i < a.length; i++) { | |
if (a[i].classList.contains('jk-current')) { | |
var next = (i + inc) % a.length; | |
a[i].classList.toggle('jk-current'); | |
a[next].classList.toggle('jk-current'); | |
break; | |
} | |
} | |
}; | |
/* Do jk navigation */ | |
$('head').append('<style>.jk-current {outline: 1px solid navy;}</style>'); | |
var arts = document.getElementsByTagName("article"); | |
if (arts.length > 3) { | |
arts[2].classList.add('jk-current'); | |
jk(arts, 1); | |
} | |
$(document.body).keypress(function (event) { | |
// ignore keypresses in input fields | |
if (document.activeElement.tagName == 'INPUT') { | |
return; | |
} | |
if (arts.length === 0) { | |
return; | |
} | |
var key = String.fromCharCode(event.which).toUpperCase(); | |
if (key === 'J' || key === ' ') { // J or space for next | |
jk(arts, 1); | |
} else if (key == 'K') { // K for back | |
jk(arts, -1); | |
} else if (key == 'O' || event.which == 13) { | |
var toClick = $(".jk-current").find('a')[0]; | |
console.log("toClick = " + toClick); | |
toClick.click(); | |
} else { | |
return; | |
} | |
//scroll .jk-current to top of window | |
$('html, body').animate({ | |
scrollTop: $(".jk-current").offset().top - 100 | |
}, 200); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment