-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkonami.js
52 lines (48 loc) · 1.37 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
45
46
47
48
49
50
51
52
var Konami = function () {
'use strict';
var _CODE = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13],
_CODE_LEN = _CODE.length,
_listenerTarget = null,
_onSuccess = null,
next = 0,
_keydown_listener = function (e) {
if(e.keyCode === _CODE[next]) {
next += 1;
if (next === _CODE_LEN) {
_onSuccess();
next = 0;
}
} else {
next = 0;
}
},
_addEventListeners = function () {
if (_listenerTarget.addEventListener) {
_listenerTarget.addEventListener('keydown', _keydown_listener, false);
} else if (_listenerTarget.attachEvent) {
_listenerTarget.attachEvent('onkeydown', _keydown_listener);
} else {
if (typeof(_listenerTarget.onkeydown) === 'function') {
var preservedListenerTargetFunction = _listenerTarget.onkeydown;
_listenerTarget.onkeydown = function(e) {
preservedListenerTargetFunction(e);
_keydown_listener(e);
};
} else {
_listenerTarget.onkeydown = _keydown_listener;
}
}
};
return {
onSuccess : function () {},
listenerTarget : window,
init : function () {
_onSuccess = this.onSuccess;
_listenerTarget = this.listenerTarget;
_addEventListeners();
}
};
};
if (typeof(module) !== 'undefined') {
module.exports = Konami;
}