forked from 1j01/jspaint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkonami.js
44 lines (34 loc) · 1.09 KB
/
konami.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
var Konami = {};
(function() {
var afterSequence = function(sequence, action) {
var index = 0;
return function(event) {
var matchedKey = event.keyCode === sequence[index];
// if it didn't match, reset and try matching against the first key
if (!matchedKey) {
index = 0;
matchedKey = event.keyCode === sequence[index];
}
if (matchedKey) {
index += 1;
// fix for Firefox with "Search for text when you start typing" enabled
// https://support.mozilla.org/en-US/kb/search-contents-current-page-text-or-links
// prevent the default (opening Quick Search) for B and A,
// which are luckily at the end of the Konami Code sequence
// (otherwise it could prevent typing A and B in text fields unwantedly)
if (event.keyCode === 66 || event.keyCode === 65) {
event.preventDefault();
}
if (index === sequence.length) {
// reset when sequence completed
index = 0;
// fire action
action();
}
}
};
};
Konami.code = function(action) {
return afterSequence([38, 38, 40, 40, 37, 39, 37, 39, 66, 65], action);
};
}());