konami.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Konami-JS ~
  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.0 (1/21/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
  12. */
  13. var Konami = function(callback) {
  14. var konami= {
  15. addEvent:function ( obj, type, fn, ref_obj )
  16. {
  17. if (obj.addEventListener)
  18. obj.addEventListener( type, fn, false );
  19. else if (obj.attachEvent)
  20. {
  21. // IE
  22. obj["e"+type+fn] = fn;
  23. obj[type+fn] = function() { obj["e"+type+fn]( window.event,ref_obj ); }
  24. obj.attachEvent( "on"+type, obj[type+fn] );
  25. }
  26. },
  27. input:"",
  28. pattern:"3838404037393739666513",
  29. load: function(link) {
  30. this.addEvent(document,"keydown", function(e,ref_obj) {
  31. if (ref_obj) konami = ref_obj; // IE
  32. konami.input+= e ? e.keyCode : event.keyCode;
  33. if (konami.input.length > konami.pattern.length) konami.input = konami.input.substr((konami.input.length - konami.pattern.length));
  34. if (konami.input == konami.pattern) {
  35. konami.code(link);
  36. konami.input="";
  37. return;
  38. }
  39. },this);
  40. this.iphone.load(link)
  41. },
  42. code: function(link) { window.location=link},
  43. iphone:{
  44. start_x:0,
  45. start_y:0,
  46. stop_x:0,
  47. stop_y:0,
  48. tap:false,
  49. capture:false,
  50. orig_keys:"",
  51. keys:["UP","UP","DOWN","DOWN","LEFT","RIGHT","LEFT","RIGHT","TAP","TAP","TAP"],
  52. code: function(link) { konami.code(link);},
  53. load: function(link){
  54. this.orig_keys = this.keys;
  55. konami.addEvent(document,"touchmove",function(e){
  56. if(e.touches.length == 1 && konami.iphone.capture===true){
  57. var touch = e.touches[0];
  58. konami.iphone.stop_x = touch.pageX;
  59. konami.iphone.stop_y = touch.pageY;
  60. konami.iphone.tap = false;
  61. konami.iphone.capture=false;
  62. konami.iphone.check_direction();
  63. }
  64. });
  65. konami.addEvent(document,"touchend",function(evt){
  66. if (konami.iphone.tap===true) konami.iphone.check_direction(link);
  67. },false);
  68. konami.addEvent(document,"touchstart", function(evt){
  69. konami.iphone.start_x = evt.changedTouches[0].pageX
  70. konami.iphone.start_y = evt.changedTouches[0].pageY
  71. konami.iphone.tap = true
  72. konami.iphone.capture = true
  73. });
  74. },
  75. check_direction: function(link){
  76. var x_magnitude = Math.abs(this.start_x-this.stop_x)
  77. var y_magnitude = Math.abs(this.start_y-this.stop_y)
  78. var x = ((this.start_x-this.stop_x) < 0) ? "RIGHT" : "LEFT";
  79. var y = ((this.start_y-this.stop_y) < 0) ? "DOWN" : "UP";
  80. var result = (x_magnitude > y_magnitude) ? x : y;
  81. result = (this.tap===true) ? "TAP" : result;
  82. if (result==this.keys[0]) this.keys = this.keys.slice(1,this.keys.length)
  83. if (this.keys.length==0) {
  84. this.keys=this.orig_keys;
  85. this.code(link)
  86. }
  87. }
  88. }
  89. }
  90. typeof callback === "string" && konami.load(callback);
  91. if(typeof callback === "function") {
  92. konami.code = callback;
  93. konami.load();
  94. }
  95. return konami;
  96. }