-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink-rel-next-prev.js
More file actions
166 lines (128 loc) · 2.92 KB
/
link-rel-next-prev.js
File metadata and controls
166 lines (128 loc) · 2.92 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// ==UserScript==
// @name Link Rel Next Prev
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://*/*
// @match https://*/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function onkey(key, next) {
next(attach(curry(boom, condition)));
function condition(event) {
return event.key === key;
}
}
function boom(condition, action, event) {
if (condition(event)) action();
}
function attach(listener) {
return attach_;
function attach_(condition) {
document.body.addEventListener('keyup', listener);
}
}
function curry(f, ...args) {
return curry_;
function curry_(...args_) {
return f(...args, ...args_);
}
}
foo(onkey('ArrowRight', perhaps(navigate(to(link('next'))))));
foo(onkey('ArrowLeft', perhaps(navigate(to(link('prev'))))));
function onkey(key, next) {
next(attach);
function attach(action) {
document.addEventListener('keyup', listener(condition, action));
}
function condition(event) {
return event.key === key;
}
}
function listener(condition, action) {
return listener_;
function listener_(event) {
if (condition(event)) action();
}
}
function permitted(event) {
return (
event.currentTarget === event.target &&
!event.defaultPrevented &&
!event.altKey &&
!event.ctrlKey &&
!event.metaKey &&
!event.shiftKey
);
}
onkey('ArrowRight', perhaps(navigate(to(link('next')))));
onkey('ArrowLeft', perhaps(navigate(to(link('prev')))));
function onkey(key, next) {
next(onkey_);
function onkey_(action) {
document.addEventListener('keyup', listener);
function listener(event) {
if (event.key === key) action();
}
}
}
function link(rel) {
return document.querySelector('head > link[rel=' + rel + '][href]');
}
function to(link) {
return pass(cond(link, extract(href, link), nop));
}
function href(link) {
return link.href;
}
function extract(f, x) {
return extract_;
function extract_() {
return f(x);
}
}
function nop() {
return nop;
}
function cond(x, f, g) {
return x ? f(x) : g(x);
}
function pass(x) {
return pass_;
function pass_(f) {
f(x);
}
}
function navigate(where) {
return navigate_;
function navigate_() {
window.location.href = where;
}
}
////////
function onkey(key, link) {
if (!link) return;
document.addEventListener('keyup', listener);
function listener(event) {
if (active() && targeted() && matches()) navigate();
function active() {
return !event.defaultPrevented;
}
function targeted() {
// return event.currentTarget === event.target;
return event.target === document.body;
}
function matches() {
return event.key === key && modified();
}
function modified() {
return (
!event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey
);
}
}
}
})();