Reddit: jump posts with J and K keys
Adds J and K keyboard shortcuts that smoothly scroll the feed to the next or previous post.
Code to copy
// Jump between feed posts with the J and K keys
function posts() {
return Array.prototype.slice.call(document.querySelectorAll('shreddit-post'));
}
function current(list) {
return list.findIndex(function (p) {
const r = p.getBoundingClientRect();
return r.top >= -2 && r.top < window.innerHeight / 2;
});
}
document.addEventListener('keydown', function (e) {
if (e.ctrlKey || e.metaKey || e.altKey) { return; }
const tag = (e.target.tagName || '').toLowerCase();
if (tag === 'input' || tag === 'textarea' || e.target.isContentEditable) { return; }
if (e.key !== 'j' && e.key !== 'k') { return; }
const list = posts();
if (!list.length) { return; }
let i = current(list);
if (i < 0) { i = 0; }
i += (e.key === 'j' ? 1 : -1);
i = Math.max(0, Math.min(list.length - 1, i));
list[i].scrollIntoView({ behavior: 'smooth', block: 'start' });
});
How to use this example
- Copy the code with the button above.
- Install JustZix (2 minutes) and open the extension on the target page.
- Add a new rule matching that page.
- Paste the code into the rule's JavaScript panel and save — it runs on every page visit.
Rate this example
No ratings yet — be the first.