Arrow Keys: Next/Prev Chapter

Arrow Key Keyboard shortcuts for multiple manga reader websites (next/prev chapter)

目前為 2023-07-18 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Arrow Keys: Next/Prev Chapter
// @namespace    https://github.com/Astropilot
// @version      0.1.0
// @description  Arrow Key Keyboard shortcuts for multiple manga reader websites (next/prev chapter)
// @author       Astropilot
// @license      MIT
// @homepage     https://github.com/Astropilot/webtoon_userscripts
// @homepageURL  https://github.com/Astropilot/webtoon_userscripts
// @supportURL   https://github.com/Astropilot/webtoon_userscripts/issues
// @run-at       document-end
// @grant        none
// @noframes
// @match        *://*.asurascans.com/*-chapter-*
// @match        *://*.asura.gg/*-chapter-*
// @match        *://*.manga-scans.com/chapter/*
// @match        *://*.reaperscans.com/comics/*/chapters/*
// @match        *://*.webtoons.com/*/viewer*episode_no=*
// @match        *://*.xcalibrscans.com/*-chapter-*
// @match        *://*.mangakakalot.com/chapter/*
// @match        *://*.chapmanganato.com/manga-*/chapter-*
// @match        *://*.scyllascans.org/read/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/psl.min.js#sha256-pGXYc481WIYNZUsKubKxCxQUydhNrlM5S8g5eMU8fdw=
// ==/UserScript==

(function () {
  "use strict";

  // Selectors should point to link (<a href/>) or <button> that redirect to prev/next chapter.
  const navigationSelectorsPerDomains = [
    {
      hosts: ["asurascans.com", "asura.gg", "xcalibrscans.com"],
      selectors: {
        prev: "a.ch-prev-btn",
        next: "a.ch-next-btn"
      }
    },
    {
      hosts: ["manga-scans.com"],
      selectors: {
        prev: "div.prev-post > a",
        next: "div.next-post > a"
      }
    },
    {
      hosts: ["reaperscans.com"],
      selectors: {
        prev: "main nav:nth-of-type(1) div.flex:nth-of-type(1) > a",
        next: "main nav:nth-of-type(1) div.flex:nth-of-type(3) > a:nth-of-type(2)"
      },
    },
    {
      hosts: ["webtoons.com"],
      selectors: {
        prev: "a._prevEpisode",
        next: "a._nextEpisode"
      }
    },
    {
      hosts: ["mangakakalot.com"],
      selectors: {
        prev: ".btn-navigation-chap > a.next", // Not an error, next/back classes are really reversed...
        next: ".btn-navigation-chap > a.back"
      }
    },
    {
      hosts: ["chapmanganato.com"],
      selectors: {
        prev: ".navi-change-chapter-btn > a.navi-change-chapter-btn-prev",
        next: ".navi-change-chapter-btn > a.navi-change-chapter-btn-next"
      }
    },
    {
      hosts: ["scyllascans.org"],
      selectors: {
        prev: "div[class*='BottomActionsWrapper'] > button:nth-of-type(1)",
        next: "div[class*='BottomActionsWrapper'] > button:nth-of-type(2)"
      }
    }
  ];

  // We extract top domain from hostname without subdomains
  const currentDomain = psl.get(window.location.hostname);

  if (currentDomain === null || currentDomain.length === 0) {
    console.warn("[Arrow Keys UserScript] Failed to parse current domain!");
    return;
  }

  let rule = null;
  for (const domainsRule of navigationSelectorsPerDomains) {
    if (domainsRule.hosts.includes(currentDomain)) {
      rule = domainsRule;
      break;
    }
  }

  if (rule === null) {
    console.warn("[Arrow Keys UserScript] Failed to find selectors rule!");
    return;
  }

  document.addEventListener("keyup", (event) => {
    if (event.target && (event.target.matches("input") || event.target.matches("textarea") || event.target.isContentEditable)) {
      // Do nothing when inside a text/editable field.
      return;
    }
    if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
      // Do nothing when modifiers are held
      return;
    }

    if (event.code === "ArrowLeft") {
      document.querySelector(rule.selectors.prev)?.click();
    } else if (event.code === "ArrowRight") {
      document.querySelector(rule.selectors.next)?.click();
    }
  });
})();