konami.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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: https://github.com/georgemandis/konami-js
  6. * Copyright (c) 2009 George Mandis (https://george.mand.is)
  7. * Version: 1.6.3 (11/11/2021)
  8. * Licensed under the MIT License (http://opensource.org/licenses/MIT)
  9. * Tested in: Safari 4+, Google Chrome 4+, Firefox 3+, IE7+, Mobile Safari 2.2.1+ and Android
  10. */
  11. var Konami = function (callback) {
  12. var konami = {
  13. addEvent: function (obj, type, fn, ref_obj) {
  14. if (obj.addEventListener)
  15. obj.addEventListener(type, fn, false);
  16. else if (obj.attachEvent) {
  17. // IE
  18. obj["e" + type + fn] = fn;
  19. obj[type + fn] = function () {
  20. obj["e" + type + fn](window.event, ref_obj);
  21. }
  22. obj.attachEvent("on" + type, obj[type + fn]);
  23. }
  24. },
  25. removeEvent: function (obj, eventName, eventCallback) {
  26. if (obj.removeEventListener) {
  27. obj.removeEventListener(eventName, eventCallback);
  28. } else if (obj.attachEvent) {
  29. obj.detachEvent(eventName);
  30. }
  31. },
  32. input: "",
  33. pattern: "38384040373937396665",
  34. keydownHandler: function (e, ref_obj) {
  35. if (ref_obj) {
  36. konami = ref_obj;
  37. } // IE
  38. konami.input += e ? e.keyCode : event.keyCode;
  39. if (konami.input.length > konami.pattern.length) {
  40. konami.input = konami.input.substr((konami.input.length - konami.pattern.length));
  41. }
  42. if (konami.input === konami.pattern) {
  43. konami.code(konami._currentLink);
  44. konami.input = '';
  45. e.preventDefault();
  46. return false;
  47. }
  48. },
  49. load: function (link) {
  50. this._currentLink = link;
  51. this.addEvent(document, "keydown", this.keydownHandler, this);
  52. this.iphone.load(link);
  53. },
  54. unload: function () {
  55. this.removeEvent(document, 'keydown', this.keydownHandler);
  56. this.iphone.unload();
  57. },
  58. code: function (link) {
  59. window.location = link
  60. },
  61. iphone: {
  62. start_x: 0,
  63. start_y: 0,
  64. stop_x: 0,
  65. stop_y: 0,
  66. tap: false,
  67. capture: false,
  68. orig_keys: "",
  69. keys: ["UP", "UP", "DOWN", "DOWN", "LEFT", "RIGHT", "LEFT", "RIGHT", "TAP", "TAP"],
  70. input: [],
  71. code: function (link) {
  72. konami.code(link);
  73. },
  74. touchmoveHandler: function (e) {
  75. if (e.touches.length === 1 && konami.iphone.capture === true) {
  76. var touch = e.touches[0];
  77. konami.iphone.stop_x = touch.pageX;
  78. konami.iphone.stop_y = touch.pageY;
  79. konami.iphone.tap = false;
  80. konami.iphone.capture = false;
  81. konami.iphone.check_direction();
  82. }
  83. },
  84. touchendHandler: function () {
  85. konami.iphone.input.push(konami.iphone.check_direction());
  86. if (konami.iphone.input.length > konami.iphone.keys.length) konami.iphone.input.shift();
  87. if (konami.iphone.input.length === konami.iphone.keys.length) {
  88. var match = true;
  89. for (var i = 0; i < konami.iphone.keys.length; i++) {
  90. if (konami.iphone.input[i] !== konami.iphone.keys[i]) {
  91. match = false;
  92. }
  93. }
  94. if (match) {
  95. konami.iphone.code(konami._currentLink);
  96. }
  97. }
  98. },
  99. touchstartHandler: function (e) {
  100. konami.iphone.start_x = e.changedTouches[0].pageX;
  101. konami.iphone.start_y = e.changedTouches[0].pageY;
  102. konami.iphone.tap = true;
  103. konami.iphone.capture = true;
  104. },
  105. load: function (link) {
  106. this.orig_keys = this.keys;
  107. konami.addEvent(document, "touchmove", this.touchmoveHandler);
  108. konami.addEvent(document, "touchend", this.touchendHandler, false);
  109. konami.addEvent(document, "touchstart", this.touchstartHandler);
  110. },
  111. unload: function () {
  112. konami.removeEvent(document, 'touchmove', this.touchmoveHandler);
  113. konami.removeEvent(document, 'touchend', this.touchendHandler);
  114. konami.removeEvent(document, 'touchstart', this.touchstartHandler);
  115. },
  116. check_direction: function () {
  117. x_magnitude = Math.abs(this.start_x - this.stop_x);
  118. y_magnitude = Math.abs(this.start_y - this.stop_y);
  119. x = ((this.start_x - this.stop_x) < 0) ? "RIGHT" : "LEFT";
  120. y = ((this.start_y - this.stop_y) < 0) ? "DOWN" : "UP";
  121. result = (x_magnitude > y_magnitude) ? x : y;
  122. result = (this.tap === true) ? "TAP" : result;
  123. return result;
  124. }
  125. }
  126. }
  127. typeof callback === "string" && konami.load(callback);
  128. if (typeof callback === "function") {
  129. konami.code = callback;
  130. konami.load();
  131. }
  132. return konami;
  133. };
  134. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  135. module.exports = Konami;
  136. } else {
  137. if (typeof define === 'function' && define.amd) {
  138. define([], function() {
  139. return Konami;
  140. });
  141. } else {
  142. window.Konami = Konami;
  143. }
  144. }