-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (41 loc) · 1.16 KB
/
index.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
var konami = function(opts) {
if (typeof opts.once === "undefined") {
opts.once = true;
}
if (typeof opts.useCapture === "undefined") {
opts.useCapture = true;
}
if (typeof opts.callback !== "function") {
throw new Error("Konami: callback is not a function.");
return;
}
var ran = false;
var keypresses = [];
var KONAMI_CODE = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65];
document.addEventListener('keydown', function(e) {
if (opts.once && ran) {
return;
}
var key = (function(e) {
var event = e || window.event;
return (event.keyCode || event.which);
})(e);
// if first button isn't up, return
if (keypresses.length == 0 && key != 38) {
return;
// if valid konami code character and keypresses available
} else if (keypresses.length < 10 && /37|38|39|40|65|66/.test(key)) {
keypresses.push(key);
if (keypresses.length == 10
&& JSON.stringify(keypresses) == JSON.stringify(KONAMI_CODE)) {
opts.callback();
if (opts.once) {
ran = true;
}
}
} else {
keypresses = [];
}
}, opts.useCapture);
};
module.exports = konami;