VimKeys Navigation for WeebCentral

hjkl navigation within WeebCentral's reader. <Alt-c> to show reader's settings. <Alt-g> to show page select dialog.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
//
// @name        VimKeys Navigation for WeebCentral
// @namespace   Violentmonkey Scripts
// @match       https://weebcentral.com/chapters/*
// @grant       none
// @version     0.2
// @author      Sadist
// @license     MIT
// @description hjkl navigation within WeebCentral's reader. <Alt-c> to show reader's settings. <Alt-g> to show page select dialog.
// ==/UserScript==

const scrollLines = 35;
const ratioExp = 2.6;
const keyList = ['g','c','h','j','k','l','f'];


function isInputActive() {
	return (document.activeElement.tagName === "INPUT");
}

function noModifierKey(event) {
	return (!((event.altKey) || (event.ctrlKey) || (event.metaKey) || (event.shiftKey)));
}

document.addEventListener("keydown", (event) => {
	if ( isInputActive() ) {
		return;
	}
	if ( !keyList.includes(event.key) ) {
		console.log('key not in list')
		return;
	}
	if ( noModifierKey(event) ) {
		switch (event.key) {
			case 'h':
				document.getElementById('nav-top').querySelector('div > button:nth-child(4)').click();
				break;
			case 'l':
				document.getElementById('nav-top').querySelector('div > button:nth-child(8)').click();
				break;
			case 'k':
				scrollByLines(-scrollLines * Math.pow(window.devicePixelRatio, ratioExp));
				break;
			case 'j':
				scrollByLines(scrollLines * Math.pow(window.devicePixelRatio, ratioExp));
				break;
		}
		return;
	}
	if ( (event.key === 'g') && (event.altKey) ) {
		document.getElementById('nav-top').querySelector('div > button:nth-child(6)').click();
		return;
	}
	if ( (event.key === 'c') && (event.altKey) ) {
		preference_modal.showModal()
		return;
	}
	if ( (event.key === 'f') && (event.ctrlKey) ) {
		event.preventDefault();
		document.getElementById('quick-search-input').focus();
		return;
	}
});