← All examples

JavaScript Navigation quora.com

Quora: jump between answers with J/K

Adds J and K keyboard shortcuts that smoothly scroll the page to the next or previous answer.

Code to copy

// Jump between answers with the J and K keys
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 answers = Array.prototype.slice.call(
    document.querySelectorAll('div[class*="puppeteer_test_answer_content"]')
  );
  if (!answers.length) { return; }
  const y = window.scrollY + 80;
  let idx = answers.findIndex(function (a) {
    return a.getBoundingClientRect().top + window.scrollY > y;
  });
  if (e.key === 'k') { idx = idx - 2; }
  idx = Math.max(0, Math.min(answers.length - 1, idx < 0 ? answers.length - 1 : idx));
  answers[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