Skip to content

Commit

Permalink
Added ability to pass characters instead of keyCodes. Reworked Listen
Browse files Browse the repository at this point in the history
method to convert incoming characters to lower or upper case and added
type checking prior to executing callbacks. Removed potential infinite
loop in Listen by unsetting captured keys prior to executing callback
and hooks.

Added ignored keys property to handle shift+letter uppercase letters.

Amended Egg constructor to allow new easter egg to be added directly via
the constructor.

Added bower config to allow bower publishing/installation, added Grunt tasks to minify
project.
  • Loading branch information
Rob McVey committed Apr 16, 2015
1 parent 832f3bb commit ac52c79
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
17 changes: 17 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = function(grunt) {
grunt.initConfig({
uglify: {
options: {
mangle: true
},
target: {
files: {
'egg.min.js': ['egg.js']
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('build', ['uglify']);
}
22 changes: 22 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "egg.js",
"main": "egg.js",
"version": "0.0.1",
"homepage": "https://github.com/mikeflynn/egg.js",
"authors": [
"Mike Flynn <[email protected]>"
],
"description": "A simple javascript library to add easter eggs to web pages.",
"keywords": [
"easter eggs",
"konami code"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
105 changes: 80 additions & 25 deletions egg.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
// thatmikeflynn.com/egg.js/

function Egg() {
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 = {
"up": 38, "down": 40, "left": 37, "right": 39, "enter": 13, "space": 32, "ctrl": 7, "alt": 8, "tab": 9
},
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(Number.isInteger(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: keys, fn: fn, metadata: metadata});
this.eggs.push({keys: this.__toCharCodes(keys), fn: fn, metadata: metadata});

return this;
}
Expand All @@ -22,36 +64,49 @@ Egg.prototype.AddHook = function(fn) {

Egg.prototype.Listen = function() {
if(window.addEventListener) {
window.addEventListener("keydown", function(e) {
this.kps.push(e.keyCode);
window.addEventListener("keydown", function keydownHandler(e) {
var keyCode = e.keyCode;
// keydown defaults all letters to uppercase
if(keyCode >= 65 && keyCode <= 90) {
if(!e.shiftKey) {
// convert to lower case letter
keyCode = keyCode + 32;
}
}

this.eggs.forEach(function(v, i, a) {
if(this.kps.toString().indexOf(this.eggs[i].keys) >= 0) {
// Call the fired egg function
this.activeEgg = this.eggs[i];
this.eggs[i].fn();
// make sure that it's not an ignored key (shift for one)
if(this.ignoredKeys.indexOf(keyCode) === -1) {
this.kps.push(keyCode);
}

// Call the hooks
this.hooks.forEach(function(hook, i, a) {
hook.call(this);
}.bind(this));
this.eggs.forEach(function(currentEgg, i) {
var foundEgg = this.kps.toString().indexOf(currentEgg.keys) >= 0;

// Reset
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 = '';
}
}.bind(this));
}, this);

}.bind(this));
}
}

// Example:
// var egg = new Egg();
// egg.AddCode("38,38,40,40,37,39,37,39,66,65", function() {
// alert("Konami!");
// }, "konami-code");
// egg.AddHook(function(){
// console.log("Hook called for: " + this.activeEgg.keys);
// console.log(this.activeEgg.metadata);
// });
// egg.Listen();
// EGGSAMPLE

This comment has been minimized.

Copy link
@matharden

matharden Apr 24, 2015

👍 lol

var egg = new Egg();
egg
.AddCode("up,up,down,down,left,right,left,right,b,a", function() {
alert("Konami!");
}, "konami-code")
.AddHook(function(){
console.log("Hook called for: " + this.activeEgg.keys);
console.log(this.activeEgg.metadata);
}).Listen();
1 change: 1 addition & 0 deletions egg.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "egg.js",
"version": "0.0.1",
"description": "A simple javascript library to add easter eggs to web pages.",
"main": "Gruntfile.js",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-uglify": "^0.9.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/mikeflynn/egg.js.git"
},
"keywords": [
"easter eggs",
"konami code"
],
"author": "Mike Flynn",
"license": "ISC",
"bugs": {
"url": "https://github.com/mikeflynn/egg.js/issues"
},
"homepage": "https://github.com/mikeflynn/egg.js"
}

0 comments on commit ac52c79

Please sign in to comment.