-
Notifications
You must be signed in to change notification settings - Fork 0
/
egg.js
278 lines (242 loc) · 8.3 KB
/
egg.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// thatmikeflynn.com/egg.js/
/*
Copyright (c) 2015 Mike Flynn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
function Egg(/* keySequence, fn, metadata */) {
this.eggs = [];
this.hooks = [];
this.kps = [];
this.activeEgg = '';
// for now we'll just ignore the shift key to allow capital letters
this.ignoredKeys = [16];
if(arguments.length) {
this.AddCode.apply(this, arguments);
}
}
// attempt to call passed function bound to Egg object instance
Egg.prototype.__execute = function(fn) {
return typeof fn === 'function' && fn.call(this);
}
// converts literal character values to keyCodes
Egg.prototype.__toCharCodes = function(keys) {
var special = {
"slash": 191, "up": 38, "down": 40, "left": 37, "right": 39, "enter": 13, "space": 32, "ctrl": 17, "alt": 18, "tab": 9, "esc": 27
},
specialKeys = Object.keys(special);
if(typeof keys === 'string') {
// make sure there isn't any whitespace
keys = keys.split(',').map(function(key){
return key.trim();
});
}
var characterKeyCodes = keys.map(function(key) {
// check if it's already a keycode
if(key === parseInt(key, 10)) {
return key;
}
// lookup in named key map
if(specialKeys.indexOf(key) > -1) {
return special[key];
}
// it's a letter, return the char code for it
return (key).charCodeAt(0);
});
return characterKeyCodes.join(',');
}
// Keycode lookup: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
Egg.prototype.AddCode = function(keys, fn, metadata) {
this.eggs.push({keys: this.__toCharCodes(keys), fn: fn, metadata: metadata});
return this;
}
Egg.prototype.AddHook = function(fn) {
this.hooks.push(fn);
return this;
}
Egg.prototype.handleEvent = function(e) {
var keyCode = e.which;
var isLetter = keyCode >= 65 && keyCode <= 90;
/*
This prevents find as you type in Firefox.
Only prevent default behavior for letters A-Z.
I want keys like page up/down to still work.
*/
if ( e.type === "keydown" && !e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey ) {
var tag = e.target.tagName;
if ( ( tag === "HTML" || tag === "BODY" ) && isLetter ) {
e.preventDefault();
return;
}
}
if ( e.type === "keyup" && this.eggs.length > 0 ) {
// keydown defaults all letters to uppercase
if(isLetter) {
if(!e.shiftKey) {
// convert to lower case letter
keyCode = keyCode + 32;
}
}
// make sure that it's not an ignored key (shift for one)
if(this.ignoredKeys.indexOf(keyCode) === -1) {
this.kps.push(keyCode);
}
this.eggs.forEach(function(currentEgg, i) {
var foundEgg = this.kps.toString().indexOf(currentEgg.keys) >= 0;
if(foundEgg) {
// Reset keys; if more keypresses occur while the callback is executing, it could retrigger the match
this.kps = [];
// Set the activeEgg to this one
this.activeEgg = currentEgg;
// if callback is a function, call it
this.__execute(currentEgg.fn, this);
// Call the hooks
this.hooks.forEach(this.__execute, this);
this.activeEgg = '';
}
}, this);
}
};
Egg.prototype.Listen = function() {
// Standards compliant only. Don't waste time on IE8.
if ( document.addEventListener !== void 0 ) {
document.addEventListener( "keydown", this, false );
document.addEventListener( "keyup", this, false );
}
return this;
};
Egg.prototype.listen = Egg.prototype.Listen;
Egg.prototype.addCode = Egg.prototype.AddCode;
Egg.prototype.addHook = Egg.prototype.AddHook;
//EGGSAMPLE
let home = new Egg();
home
.AddCode("h,o,m,e", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/Colorful Eggs and Bunny Illustrative Easter Animated Zoom Virtual Background.gif");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
let egg = new Egg();
egg
.AddCode("e,a,s,t,e,r", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/pexels-george-dolgikh-giftpunditscom-2072158.jpg");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
//egg2
let egg2 = new Egg();
egg2
.AddCode("h,u,n,t", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/eggHunt.png");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
let egg3 = new Egg();
egg3
.AddCode("b,u,n,n,y", function() {
window.location.href =
"https://www.novelgames.com/en/easteregghunt/";
}
).Listen();
let egg4 = new Egg();
egg4
.AddCode("e,g,g", function() {
window.location.href =
"https://www.youtube.com/watch?v=r5savRs44T8";
}
).Listen();
let egg5 = new Egg();
egg
.AddCode("l,e,t,t,h,e,g,a,m,e,s,b,e,g,i,n", function() {
window.location.href =
"https://arcadespot.com/game/toon-egg-hunt/";
}
).Listen();
let egg6 = new Egg();
egg6
.AddCode("i,t,t,s", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/egg2.png");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
let egg7 = new Egg();
egg7
.AddCode("b,y,e", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/end.gif");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
let egg8 = new Egg();
egg8
.AddCode("e,n,d", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/end.gif");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
let egg9 = new Egg();
egg9
.AddCode("o,v,e,r", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/end.gif");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
let egg10 = new Egg();
egg10
.AddCode("q,u,i,t", function() {
var body = document.getElementById('container');
var modal = document.createElement('img');
modal.setAttribute("class", "myModal");
modal.setAttribute("src", "assets/images/end.gif");
modal.setAttribute("alt", "egg 1");
body.appendChild(modal);
}
).Listen();
console.log("bye, end, over, quit");
console.log("home");
console.log("hunt");
console.log("easter");
console.log("egg");
console.log("bunny");
console.log("let the games begin")
console.log("itts");