← All examples

JavaScript Productivity x.com

X: scroll tweets with J/K keys

Lets you move smoothly between tweets with the J (down) and K (up) keys without touching the mouse.

Code to copy

// Smoothly scroll tweet-by-tweet with the J and K keys
function tweets() {
  return Array.prototype.slice.call(document.querySelectorAll('[data-testid="tweet"]'));
}
function current(list) {
  for (let i = 0; i < list.length; i++) {
    if (list[i].getBoundingClientRect().top > 60) { return i; }
  }
  return list.length - 1;
}
document.addEventListener('keydown', function (e) {
  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 = tweets();
  if (!list.length) { return; }
  let idx = current(list);
  idx += (e.key === 'j') ? 1 : -1;
  idx = Math.max(0, Math.min(list.length - 1, idx));
  list[idx].scrollIntoView({ behavior: 'smooth', block: 'start' });
});

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. 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.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases