3 * :: Now with support for touch events and multiple instances for
4 * :: those situations that call for multiple easter eggs!
5 * Code: http://konami-js.googlecode.com/
6 * Examples: http://www.snaptortoise.com/konami-js
7 * Copyright (c) 2009 George Mandis (georgemandis.com, snaptortoise.com)
8 * Version: 1.4.1 (3/1//2013)
9 * Licensed under the GNU General Public License v3
10 * http://www.gnu.org/copyleft/gpl.html
11 * Tested in: Safari 4+, Google Chrome 4+, Firefox 3+, IE7+, Mobile Safari 2.2.1 and Dolphin Browser
14 var Konami = function(callback) {
16 addEvent:function ( obj, type, fn, ref_obj )
18 if (obj.addEventListener)
19 obj.addEventListener( type, fn, false );
20 else if (obj.attachEvent)
23 obj["e"+type+fn] = fn;
24 obj[type+fn] = function() { obj["e"+type+fn]( window.event,ref_obj ); }
25 obj.attachEvent( "on"+type, obj[type+fn] );
29 pattern:"38384040373937396665",
30 load: function(link) {
31 this.addEvent(document,"keydown", function(e,ref_obj) {
32 if (ref_obj) konami = ref_obj; // IE
33 konami.input+= e ? e.keyCode : event.keyCode;
34 if (konami.input.length > konami.pattern.length) konami.input = konami.input.substr((konami.input.length - konami.pattern.length));
35 if (konami.input == konami.pattern) {
41 this.iphone.load(link);
44 code: function(link) { window.location=link},
53 keys:["UP","UP","DOWN","DOWN","LEFT","RIGHT","LEFT","RIGHT","TAP","TAP"],
54 code: function(link) { konami.code(link);},
56 this.orig_keys = this.keys;
57 konami.addEvent(document,"touchmove",function(e){
58 if(e.touches.length == 1 && konami.iphone.capture==true){
59 var touch = e.touches[0];
60 konami.iphone.stop_x = touch.pageX;
61 konami.iphone.stop_y = touch.pageY;
62 konami.iphone.tap = false;
63 konami.iphone.capture=false;
64 konami.iphone.check_direction();
67 konami.addEvent(document,"touchend",function(evt){
68 if (konami.iphone.tap==true) konami.iphone.check_direction(link);
70 konami.addEvent(document,"touchstart", function(evt){
71 konami.iphone.start_x = evt.changedTouches[0].pageX;
72 konami.iphone.start_y = evt.changedTouches[0].pageY;
73 konami.iphone.tap = true;
74 konami.iphone.capture = true;
77 check_direction: function(link){
78 x_magnitude = Math.abs(this.start_x-this.stop_x);
79 y_magnitude = Math.abs(this.start_y-this.stop_y);
80 x = ((this.start_x-this.stop_x) < 0) ? "RIGHT" : "LEFT";
81 y = ((this.start_y-this.stop_y) < 0) ? "DOWN" : "UP";
82 result = (x_magnitude > y_magnitude) ? x : y;
83 result = (this.tap==true) ? "TAP" : result;
85 if (result==this.keys[0]) this.keys = this.keys.slice(1,this.keys.length);
86 if (this.keys.length==0) {
87 this.keys=this.orig_keys;
94 typeof callback === "string" && konami.load(callback);
95 if(typeof callback === "function") {
96 konami.code = callback;