kerofenlik / Горячие клавиши Pikabu на Reddit

// ==UserScript==
// @name Горячие клавиши Pikabu на Reddit
// @description Добавляет на страницы Reddit функционал горячих клавиш Pikabu
// @icon https://i.imgur.com/SJLBRtl.png
// @match https://www.reddit.com/*
// @grant none
// @version 1.0
// @author Kerofenlik
// @license MIT
// ==/UserScript==

function getPosts(){return document.getElementsByClassName("Post")}function getPostByPosition(n,t=1){let i=getPosts();for(let r=0;r<i.length;r++){let u=i[r].offsetTop;if(t==1?u>n:u<n)return i[r]}}function getCurrentPost(n){let t=getPosts();for(let i=0;i<t.length;i++){if(Math.abs(n-t[i].offsetTop-getDefaultOffsetScroll())<1)return t[i];if(i==0&&t[i].offsetTop>n)return scrollOnElement(t[i]),t[i]}}function votePost(n,t){n.querySelector(t==-1?'button[aria-label="downvote"]':'button[aria-label="upvote"]').click()}function scrollOnElement(n){window.scroll(0,n.offsetTop+getDefaultOffsetScroll())}function openPost(n){n.click()}function closePost(){getCloseButton().click()}function getCloseButton(){return document.querySelector('button[aria-label="Close"]')}function getDefaultOffsetScroll(){return document.querySelector("header").scrollHeight-getPosts()[0].offsetTop+1}document.onkeydown=function(n){let t=document.documentElement.scrollTop;switch(n.keyCode){case 68:scrollOnElement(getPostByPosition(t));break;case 87:votePost(getCurrentPost(t));break;case 65:scrollOnElement(getPostByPosition(t,-1));break;case 83:votePost(getCurrentPost(t),-1);break;case 69:openPost(getCurrentPost(t));break;case 81:window.getComputedStyle(getCloseButton()).display!=="none"&&(closePost(),n.preventDefault())}}