-
-
Save paulroub/8ce3e8bc72d50213d2c81b0823322c29 to your computer and use it in GitHub Desktop.
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 NATO Nav Keys | |
// @namespace http://roub.net/ | |
// @version 1.0 | |
// @description 'j'/'k' to scroll to next/previous NATO answers. 'q' to scroll from an answer to its question when NATO Enhancements are in play. 'v' to open the currently-selected answer in a new window/tab. | |
// @author paulroub | |
// @match /https?:\/\/(meta\.)?stackoverflow.com\/tools\/new-answers-old-questions.*/ | |
// @include /https?:\/\/(meta\.)?stackoverflow.com\/tools\/new-answers-old-questions.*/ | |
// @updateURL https://gist.github.com/paulroub/8ce3e8bc72d50213d2c81b0823322c29/raw/natokeys.user.js | |
// @grant none | |
// ==/UserScript== | |
/* eslint-env jquery */ | |
'use strict'; | |
(function() { | |
const b = $(document.body); | |
b.keypress( function(e) { | |
var tag = e.target.tagName.toLowerCase(), | |
c = String.fromCharCode(e.which), | |
ib, it, i, | |
st = Math.ceil($(window).scrollTop()), | |
as = null, | |
noQ = false, | |
nextTop = -1, | |
prevTop = -1, | |
currentTop = -1, | |
scrollTo = -1, | |
currentTopAnswer = [], | |
a; | |
// ignore actual j, k, q in comments, etc. | |
if ( tag == 'input' || tag == 'textarea') { | |
return; | |
} | |
if ((c == 'j') || (c == 'k') || (c == 'q') || (c == 'v')) | |
{ | |
as = $('.answer'); | |
// no .answer matches == NATO enhancements not in use (or not yet) | |
// look for the unmodified answers and skip the 'q' key | |
if (as.length < 1) | |
{ | |
var pt = $('.post-text'); | |
noQ = true; | |
as = pt.get().map( | |
function(p) { | |
return $(p).closest('td'); | |
} | |
); | |
} | |
} | |
for ( i = 0; i < as.length; ++i ) | |
{ | |
a = $(as[i]); | |
it = Math.floor(a.offset().top); | |
ib = it + a.height(); | |
if ((it > st) && (nextTop < 0)) | |
{ | |
nextTop = it; | |
} | |
else if (ib <= st) | |
{ | |
prevTop = it; | |
} | |
if ((ib >= st) && (currentTop < 0) && ! noQ) | |
{ | |
const q = a.closest('tr').find('h1'); | |
currentTop = q.offset().top; | |
currentTopAnswer = a.find('.js-share-link'); | |
} | |
} | |
switch (c) | |
{ | |
case 'j': | |
scrollTo = nextTop; | |
break; | |
case 'k': | |
scrollTo = prevTop; | |
break; | |
case 'q': | |
scrollTo = currentTop; | |
break; | |
case 'v': | |
if (currentTopAnswer.length > 0) { | |
const href = currentTopAnswer.attr('href'); | |
window.open(href); | |
} | |
break; | |
} | |
if (scrollTo >= 0) | |
{ | |
$('html, body').animate({ | |
scrollTop: scrollTo | |
}, 250); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment