e621 Page Arrows

Change page on e621 using arrow keys

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
        }
    }
}