-
Notifications
You must be signed in to change notification settings - Fork 1
/
wasd_scroll.user.js
137 lines (127 loc) · 3.96 KB
/
wasd_scroll.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ==UserScript==
// @name WASD Scroll hotkeys for websites
// @namespace wasd_scroll
// @author Owyn
// @version 1.1
// @description Use WASD keys to scroll, not just arrow buttons
// @updateURL https://github.com/Owyn/WASD_Scroll/raw/master/WASD_Scroll.user.js
// @downloadURL https://github.com/Owyn/WASD_Scroll/raw/master/WASD_Scroll.user.js
// @supportURL https://github.com/Owyn/WASD_Scroll/issues
// @homepage https://github.com/Owyn/WASD_Scroll
// @run-at document-end
// @grant GM_registerMenuCommand
// @sandbox JavaScript
// @match http://*/*
// @match https://*/*
// @match file:///*/*
// ==/UserScript==
"use strict";
var scroll_window_percentage = 0.20; // from 0.0 to 1.0 (meaning 0% to 100%) how much WASD scrolls of the screen
var space_scroll_by = 0.80; // how much space it scrolls of the screen - only for when "aggressive mode" is On
var scroll_speed = "smooth"; // "smooth" / "instant" / "auto"
window.addEventListener("keydown", onkeydown, true); // before all others - change to false to make it work after all other listeners
if (typeof GM_registerMenuCommand !== "undefined")
{
GM_registerMenuCommand("WASD Scroll - Disable (this once)", disable, "w");
GM_registerMenuCommand("WASD Scroll - toggle aggressive mode for this website", wasd_toggle, "a");
}
if (typeof KeyEvent === "undefined")
{
var KeyEvent = {
DOM_VK_A: 65,
DOM_VK_D: 68,
DOM_VK_S: 83,
DOM_VK_W: 87,
DOM_VK_SPACE: 32,
};
}
var inputs = ['input', 'select', 'button', 'textarea'];
function onkeydown (b)
{
let a = (window.event) ? b.keyCode : b.which;
let by = window.innerHeight * scroll_window_percentage
if (b.altKey || b.ctrlKey || b.metaKey)
{
return;
}
let activeElement = document.activeElement;
//console.warn(activeElement);
//console.warn(b.target);
if (activeElement && (inputs.indexOf(activeElement.tagName.toLowerCase()) !== -1 || activeElement.contentEditable === "true"))
{
return;
}
let whatWeScroll = window; //document.body; // or window ?
let aggro = localStorage.getItem('WASD_AGGR') ? true : false;
let everything = aggro ? document.querySelectorAll("*") : []; // lets scroll everything, we can't miss that way!
for (let i = 0; i <= everything.length; i++)
{
switch (a)
{
case KeyEvent.DOM_VK_D:
whatWeScroll.scrollBy({
top: 0,
left: by,
behavior: scroll_speed
});
if(aggro) b.stopImmediatePropagation();
break;
case KeyEvent.DOM_VK_A:
whatWeScroll.scrollBy({
top: 0,
left: by * -1,
behavior: scroll_speed
});
if(aggro) b.stopImmediatePropagation();
break;
case KeyEvent.DOM_VK_W:
whatWeScroll.scrollBy({
top: by * -1,
left: 0,
behavior: scroll_speed
});
if(aggro) b.stopImmediatePropagation();
break;
case KeyEvent.DOM_VK_S:
whatWeScroll.scrollBy({
top: by,
left: 0,
behavior: scroll_speed
});
if(aggro) b.stopImmediatePropagation();
break;
case KeyEvent.DOM_VK_SPACE:
if(!aggro) return;
whatWeScroll.scrollBy({
top: window.innerHeight * space_scroll_by * (b.shiftKey ? -1 : 1),
left: 0,
behavior: scroll_speed
});
b.preventDefault();
b.stopImmediatePropagation();
break;
default:
return;
}
whatWeScroll = everything[i];
}
}
function disable()
{
window.removeEventListener("keydown", onkeydown, false);
console.warn("WASD Scroll disabled");
}
function wasd_toggle()
{
console.warn("WASD aggressive mode toggled");
if(localStorage.getItem('WASD_AGGR'))
{
localStorage.removeItem('WASD_AGGR');
alert("OFF - now WASD scrolling is back to normal");
}
else
{
localStorage.setItem('WASD_AGGR', "1");
alert("ON - now WASD scrolling will try to scroll EVERY element on the page and block origianl WASD-site hotkeys (if present), and own spacebar scroll will be added as well, this is remembered per-site (via cookies LocalStorage)");
}
}