0

I want to make "j" behave like "arrow down".

So unless it is captured by the website, pressing "j" should make the page scroll down a bit.

Is there a way short of writing a plugin? Maybe edit userpref.js?

If a plugin is needed, I guess it just needs a custom background.js with a few lines of code to 1) capture the "j" key and 2) scroll down a bit. If that is still possible with the new web-plugins.

What is a good solution?

no_gravity
  • 579
  • 1
  • 3
  • 14
  • [Listen to key press](https://stackoverflow.com/questions/23087721/call-a-function-on-enter-key-press) and then you can use `window.scrollTo(500, 0);` – Max Gram Apr 04 '20 at 07:12

1 Answers1

2

One option is to write a userscript (which will run automatically on pageload), have it listen for j keypresses, and when detected, call window.scrollBy(0, 20):

// ==UserScript==
// @name             Scroll J
// @match            *://*/*
// @grant            none
// ==/UserScript==

window.addEventListener('keypress', (e) => {
  // don't scroll if you're typing text:
  if (e.target.matches('textarea, input')) return;

  if (e.key === 'j') window.scrollBy(0, 20);
});

You'll need a userscript manager like Tampermonkey.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • That might be an option. I guess I could throw the (exact?) same code into the background.js of a plugin? – no_gravity Apr 04 '20 at 08:27
  • Sure, you could write a browser extension too, but they're moderately more complicated and have a lot of extra baggage. If you just want to run some custom Javascript of your own on pageload, userscripts are significantly easier to manage. But if you want to *distribute* the code for other non-technical people to use as well, an extension is more friendly for those not tech-literate. – CertainPerformance Apr 04 '20 at 08:31
  • I have put it into a content script of a plugin now. Works like a charm. The plugin actually only needs 3 files (manifest.json, content.js, icon.png) so it's pretty lightweight. One issue is that it does not scroll as nicely as arrow-down when you *hold* the j key. It kind of stumbles then. – no_gravity Apr 04 '20 at 09:20
  • It probably also has too many side effects. For example typing a message in Facebook will make the page scroll. And scrolling in elements (which works with arrow-down) does behave strange. I get the feeling it is not possible via a plugin (or userscript) because it can not really tell the browser "handle j like arrow down". And trying to emulate it will run into too many side-effects. – no_gravity Apr 04 '20 at 09:36
  • You can choose pages to blacklist, on which no keypress listener is added. You could also choose pages to whitelist, such that only pages on the whitelist have the listener added. You could also exclude keypresses on certain selectors from scrolling by altering the `textarea, input` string - just add whatever selectors to which a keypress should *not* trigger scrolling to that string. – CertainPerformance Apr 04 '20 at 09:46
  • CertainPerformance: True. But that would become a rather complex project then which constantly has to catch up with new and changing websites. – no_gravity Apr 05 '20 at 10:39
  • Without manually writing a whitelist or blacklist, the problem isn't really possible to solve in the general case because websites can try to do many things on key events, but only the user (you) can say whether you'd want such an event to prevent or allow scrolling as a result. – CertainPerformance Apr 05 '20 at 10:52