WASD Scroll hotkeys for websites

Use WASD keys to scroll websites, not just arrows

目前為 2022-02-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          WASD Scroll hotkeys for websites
// @namespace     wasd_scroll
// @author        Owyn
// @version       1.0
// @description   Use WASD keys to scroll websites, not just arrows
// @supportURL    https://github.com/Owyn/WASD_Scroll/issues
// @homepage      https://github.com/Owyn/WASD_Scroll
// @run-at        document-end
// @grant         GM_registerMenuCommand
// @include       *
// ==/UserScript==

"use strict";

var by = 70;
var scroll_speed = "auto"; // "smooth" / "instant" / "auto" - same as browser settings

if (typeof GM_registerMenuCommand !== "undefined")
{
	GM_registerMenuCommand("WASD Scroll - Disable (this once)", disable, "w");
}

window.addEventListener("keydown", onkeydown, false); // after all others

if (typeof KeyEvent === "undefined")
{
	var KeyEvent = {
		DOM_VK_A: 65,
		DOM_VK_D: 68,
		DOM_VK_S: 83,
		DOM_VK_W: 87
	};
}

var inputs = ['input', 'select', 'button', 'textarea'];

function onkeydown (b)
{
	let a = (window.event) ? b.keyCode : b.which;
	
	if (b.altKey || b.ctrlKey || b.metaKey)
	{
		return;
	}
	
	let activeElement = document.activeElement;
	if (activeElement && inputs.indexOf(activeElement.tagName.toLowerCase()) !== -1)
	{
		return;
	}

	switch (a)
	{
	case KeyEvent.DOM_VK_D:
		window.scrollBy({
			top: 0,
			left: by,
			behavior: scroll_speed
		});
		break;
	case KeyEvent.DOM_VK_A:
		window.scrollBy({
			top: 0,
			left: by * -1,
			behavior: scroll_speed
		});
		break;
	case KeyEvent.DOM_VK_W:
		window.scrollBy({
			top: by * -1,
			left: 0,
			behavior: scroll_speed
		});
		break;
	case KeyEvent.DOM_VK_S:
		window.scrollBy({
			top: by,
			left: 0,
			behavior: scroll_speed
		});
		break;
	}
}

function disable()
{
	window.removeEventListener("keydown", onkeydown, false);
	console.warn("WASD Scroll disabled");
}