erinx

Konami JS

Jun 16th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Konami = function (callback) {
  2.     var konami = {
  3.         addEvent: function (obj, type, fn, ref_obj) {
  4.             if (obj.addEventListener)
  5.                 obj.addEventListener(type, fn, false);
  6.             else if (obj.attachEvent) {
  7.                 // IE
  8.                 obj["e" + type + fn] = fn;
  9.                 obj[type + fn] = function () {
  10.                     obj["e" + type + fn](window.event, ref_obj);
  11.                 }
  12.                 obj.attachEvent("on" + type, obj[type + fn]);
  13.             }
  14.         },
  15.         input: "",
  16.         pattern: "38384040373937396665",
  17.         load: function (link) {
  18.             this.addEvent(document, "keydown", function (e, ref_obj) {
  19.                 if (ref_obj) konami = ref_obj; // IE
  20.                 konami.input += e ? e.keyCode : event.keyCode;
  21.                 if (konami.input.length > konami.pattern.length)
  22.                     konami.input = konami.input.substr((konami.input.length - konami.pattern.length));
  23.                 if (konami.input == konami.pattern) {
  24.                     konami.code(link);
  25.                     konami.input = "";
  26.                     e.preventDefault();
  27.                     return false;
  28.                 }
  29.             }, this);
  30.             this.iphone.load(link);
  31.         },
  32.         code: function (link) {
  33.             window.location = link
  34.         },
  35.         iphone: {
  36.             start_x: 0,
  37.             start_y: 0,
  38.             stop_x: 0,
  39.             stop_y: 0,
  40.             tap: false,
  41.             capture: false,
  42.             orig_keys: "",
  43.             keys: ["UP", "UP", "DOWN", "DOWN", "LEFT", "RIGHT", "LEFT", "RIGHT", "TAP", "TAP"],
  44.             code: function (link) {
  45.                 konami.code(link);
  46.             },
  47.             load: function (link) {
  48.                 this.orig_keys = this.keys;
  49.                 konami.addEvent(document, "touchmove", function (e) {
  50.                     if (e.touches.length == 1 && konami.iphone.capture == true) {
  51.                         var touch = e.touches[0];
  52.                         konami.iphone.stop_x = touch.pageX;
  53.                         konami.iphone.stop_y = touch.pageY;
  54.                         konami.iphone.tap = false;
  55.                         konami.iphone.capture = false;
  56.                         konami.iphone.check_direction();
  57.                     }
  58.                 });
  59.                 konami.addEvent(document, "touchend", function (evt) {
  60.                     if (konami.iphone.tap == true) konami.iphone.check_direction(link);
  61.                 }, false);
  62.                 konami.addEvent(document, "touchstart", function (evt) {
  63.                     konami.iphone.start_x = evt.changedTouches[0].pageX;
  64.                     konami.iphone.start_y = evt.changedTouches[0].pageY;
  65.                     konami.iphone.tap = true;
  66.                     konami.iphone.capture = true;
  67.                 });
  68.             },
  69.             check_direction: function (link) {
  70.                 x_magnitude = Math.abs(this.start_x - this.stop_x);
  71.                 y_magnitude = Math.abs(this.start_y - this.stop_y);
  72.                 x = ((this.start_x - this.stop_x) < 0) ? "RIGHT" : "LEFT";
  73.                 y = ((this.start_y - this.stop_y) < 0) ? "DOWN" : "UP";
  74.                 result = (x_magnitude > y_magnitude) ? x : y;
  75.                 result = (this.tap == true) ? "TAP" : result;
  76.  
  77.                 if (result == this.keys[0]) this.keys = this.keys.slice(1, this.keys.length);
  78.                 if (this.keys.length == 0) {
  79.                     this.keys = this.orig_keys;
  80.                     this.code(link);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     typeof callback === "string" && konami.load(callback);
  87.     if (typeof callback === "function") {
  88.         konami.code = callback;
  89.         konami.load();
  90.     }
  91.  
  92.     return konami;
  93. };
Add Comment
Please, Sign In to add comment