-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.konami.js
73 lines (54 loc) · 1.32 KB
/
jquery.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Konami Code jQuery plugin
* =========================
* A jQuery Plugin for easily integrating an easter egg in your project
* to be triggered via the [Konami code].
*
*
* - Copyright 2011 8BIT, http://8bit.io
* - Copyright 2011 Marek `saji` Augustynowicz, http://github.com/marek-saji
*
* Released under the [MIT License].
*
*
* Typical usage
* -------------
*
* $.konami(function() {
* // do something amazing
* });
*
*
* [Konami code]: http://en.wikipedia.org/wiki/Konami_Code
* [MIT License]: http://www.opensource.org/licenses/mit-license.php
*/
(function ($) {
"use strict";
var konamiCode = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65],
konamiCodeLength = konamiCode.length
;
$.konami = function (cheat) {
$(window).konami(cheat);
};
$.fn.konami = function (cheat) {
return this.each(function () {
// matched keys counter, index for konamiCode array
var idx = 0;
$(this).keyup(function (evt) {
var code = evt.keyCode || evt.which;
if (code === konamiCode[idx]) {
// matched a key
idx += 1;
} else {
// failure, start counting from begining
idx = 0;
}
// matched all keys
if (idx === konamiCodeLength) {
cheat();
idx = 0;
}
}); // $(this).keyup
}); // return this.each
}; // $.fn.konami
}(jQuery));