Guest User

keyboard.user.js

a guest
Apr 21st, 2019
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // ==UserScript==
  2. // @name reddit inbox keyboard commands
  3. // @match https://*.reddit.com/message/*
  4. // @description RES style keyboard commands, without the bulk. Meant for /r/counting, and only works in inbox.
  5. // @author TehVulpez
  6. // ==/UserScript==
  7.  
  8. var siteTable = document.getElementById("siteTable");
  9.  
  10. var currentNo = 0;
  11. var currentEl;
  12.  
  13. function handleKeys(e) {
  14. if (document.activeElement.nodeName == "TEXTAREA") { // stop keyboard commands from activating while in textarea
  15. return;
  16. }
  17. else if (e.key == "j" && currentNo != 48) { // down
  18. currentEl.style.backgroundColor = "#fff";
  19. currentNo += 2;
  20. setMessage();
  21. currentEl.scrollIntoView({block:"center"});
  22. }
  23. else if (e.key == "k" && currentNo != 0) { // up
  24. currentEl.style.backgroundColor = "#fff";
  25. currentNo -= 2;
  26. setMessage();
  27. currentEl.scrollIntoView({block:"center"});
  28. }
  29. else if (e.key == "a") { // upvote
  30. currentEl.getElementsByClassName("arrow")[0].click();
  31. }
  32. else if (e.key == "z") { // downvote
  33. currentEl.getElementsByClassName("arrow")[1].click();
  34. }
  35. else if (e.key == "r" && !e.ctrlKey) { // reply
  36. currentEl.getElementsByClassName("buttons")[0].lastChild.lastChild.click();
  37. e.preventDefault();
  38. }
  39. }
  40.  
  41. function setMessage() {
  42. currentEl = siteTable.children[currentNo];
  43.  
  44. currentEl.style.backgroundColor = "#eef";
  45. }
  46.  
  47. setMessage();
  48.  
  49. addEventListener("keydown",handleKeys);
Add Comment
Please, Sign In to add comment