Pagination Keyboard Shortcut Fix

Fix unreliable pagination behavior

// ==UserScript==
// @name         Pagination Keyboard Shortcut Fix
// @namespace    https://greasyfork.org/users/65414
// @description  Fix unreliable pagination behavior
// @version      0.1
// @match        *.tumblr.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {

  (window.opera ? document.body : document).addEventListener('keydown', function(e) {

    let btns = {
      "Previous": 37,
      "Next": 39
    }

    $(Object.keys(btns)).each(function() {
      let el = $(`button[aria-label="${this}"] > :first-child`).last();
      if (e.keyCode == btns[this]) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
        if (el.closest('._3uGiA._2t9LV').length || el.length && !$('._3uGiA._2t9LV').length)
          el.click();
      }
    })
    return false;

  }, !window.opera);

})();