konami code


SUBMITTED BY: Guest

DATE: Oct. 9, 2013, 9:40 a.m.

FORMAT: JavaScript

SIZE: 3.7 kB

HITS: 1553

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

comments powered by Disqus