e621 Page Arrows

Change page on e621 using arrow keys

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         e621 Page Arrows
// @namespace    NA
// @version      0.2
// @description  Change page on e621 using arrow keys
// @author       kittywithclaws
// @include      https://e621.net/post/show/*
// @include      https://e621.net/post/index/*
// ==/UserScript==

//If it detects a Next or Previous button, add a keydown check.
//[Next >>] is used by Pool buttons, and [Next »] is used by the index. Mildly annoying inconsistent design.
var pool = false;
if(getElementsByText('Next >>').length!==0 || getElementsByText('<< Previous').length!==0){
    pool = true;
    document.body.onkeydown = function() {checkKeycode();};
}else if(getElementsByText('Next »').length!==0 || getElementsByText('« Previous').length!==0){
    document.body.onkeydown = function() {checkKeycode();};
}

//This skims the page for elements by text. Used to find Next/Prev buttons.
function getElementsByText(str, tag = 'a') {
  return Array.prototype.slice.call(document.getElementsByTagName(tag)).filter(el => el.textContent.trim() === str.trim());
}

function checkKeycode(event) {
    // handling Internet Explorer stupidity with window.event
    // @see http://stackoverflow.com/a/3985882/517705
    var keyDownEvent = event || window.event,
        keycode = (keyDownEvent.which) ? keyDownEvent.which : keyDownEvent.keyCode;

    //console.log(keycode);
    if(keycode==39 || keycode==37){
        changePage(keycode);
    }
    return false;
}
//Check if Left or Right arrow are pressed, and change page accordingly.
function changePage(keycode){
    if(keycode==39){
        //Right
        /*console.log(
            getElementsByText('Next >>')[0].href
        );*/
        if(pool){
            window.location.assign(getElementsByText('Next >>')[0].href);
        }else{
            window.location.assign(getElementsByText('Next »')[0].href);}
    }else if(keycode==37){
        //left
        /*console.log(
            getElementsByText('<< Previous')[0].href
        );*/
        if(pool){
            window.location.assign(getElementsByText('<< Previous')[0].href);
        }else{
            window.location.assign(getElementsByText('« Previous')[0].href);
        }
    }
}