Greasy Fork

Greasy Fork is available in English.

Arrow Keys: Next/Prev Chapter

Arrow Key Keyboard shortcuts for Multiple Sites (next/prev chapter)

当前为 2020-12-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Arrow Keys: Next/Prev Chapter
// @namespace    http://greasyfork.icu/users/45933
// @version      0.0.1
// @author       Fizzfaldt
// @description  Arrow Key Keyboard shortcuts for Multiple Sites (next/prev chapter)
// @run-at       document-end
// @grant        none
// @noframes
// @nocompat    Chrome
// @match        *://*.wuxiaworld.co/*
// @match        *://*.wuxiaworld.com/*
// @match        *://*.fanfiction.net/*
// ==/UserScript==

/* jshint esversion: 6 */

function xPathResultToSingleNode(x, context) {
    'use strict';
    if (!x) {
        alert("NULL xPathResult! " + context);
    }
    let first = x.iterateNext();
    if (!first) {
        alert("No xPathResult node! " + context);
        return;
    }
    let second = x.iterateNext();
    if (second != null) {
        alert("xPathResult had second node! [" + second + " ] " + context);
    }
    return first;
}

const prev = "prev";
const next = "next";

function getXpaths() {
    'use strict';

    var xpaths = {
        "wuxiaworld.co" : {
            prev : '//a[@class="prev" and @href]/@href',
            next : '//a[@class="next" and @href]/@href'
        },
        "wuxiaworld.com" : {
            prev : '//li[@class="prev"]/a[contains(@class, "btn-link") and @href]/@href',
            next : '//li[@class="next"]/a[contains(@class, "btn-link") and @href]/@href'
        }
    };

    var domain = location.hostname;
    while (domain != '') {
        if (domain in xpaths) {
            return xpaths[domain];
        }
        // Strip one subdomain and try again
        let temp = domain.split('.');
        temp.shift();
        domain = temp.join('.');
    }
    return null;
}

var pathsForHost = getXpaths();

function arrows(e) {
    'use strict';
    var result;
    switch (e.keyCode) {
        case 37: // Left
            result = document.evaluate(pathsForHost[prev], document, null, XPathResult.ANY_TYPE, null);
            break;
        case 39: // Right
            result = document.evaluate(pathsForHost[next], document, null, XPathResult.ANY_TYPE, null);
            break;
            // case 38: // Up
            // case 40: // Down
        default:
            return;
    }
    let linkHref = xPathResultToSingleNode(result);
    let href = linkHref.value;

    location.href=href;
}

if (pathsForHost) {
    document.addEventListener('keyup', arrows, false);
}