Squidi Comics Keyboard

Adds Left and Right shortcuts for navigating view pages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Squidi Comics Keyboard
// @namespace   me.kroltan.squidi-kb
// @description Adds Left and Right shortcuts for navigating view pages
// @include     http://www.squidi.net/comic/*
// @version     1
// @grant       none
// ==/UserScript==

const PREVIOUS_KEYCODE = 37;
const NEXT_KEYCODE = 39;

const textEquals = cmp => (l => l.innerText.toLowerCase() === cmp);

const links = [...document.querySelectorAll(`a[href*="${location.pathname}"]`)];
const previousEl = links.filter(textEquals("prev"))[0];
const nextEl = links.filter(textEquals("next"))[0];

const header = document.querySelector('font[size="2"]');
header.innerHTML += `
  <label title="Use arrow keys for navigation, previous/next comics">
    <input type="checkbox" />
    Enable shortcuts
  </label>
`;
const enableBox = header.querySelector("input");
enableBox.checked = JSON.parse(localStorage.getItem("shortcuts_enabled"));
enableBox.addEventListener("change", event => {
  localStorage.setItem("shortcuts_enabled", JSON.stringify(enableBox.checked));
})

if (previousEl && nextEl) {
  document.body.addEventListener("keyup", event => {
    let link = null;
    switch (event.which) {
      case PREVIOUS_KEYCODE:
        link = previousEl;
        break;
      case NEXT_KEYCODE:
        link = nextEl;
        break;
      default: break;
    }
    if (link && enableBox.checked) {
      link.click();
    }
  });
}