Skip to content

Instantly share code, notes, and snippets.

@llaine
Created September 15, 2014 17:34
Show Gist options
  • Save llaine/917b9532586cae9d3f29 to your computer and use it in GitHub Desktop.
Save llaine/917b9532586cae9d3f29 to your computer and use it in GitHub Desktop.
var Konami = function(t) {
var e = {
addEvent: function(t, e, n, i) {
t.addEventListener ? t.addEventListener(e, n, !1) : t.attachEvent && (t["e" + e + n] = n, t[e + n] = function() {
t["e" + e + n](window.event, i)
}, t.attachEvent("on" + e, t[e + n]))
},
input: "",
pattern: "38384040373937396665",
load: function(t) {
this.addEvent(document, "keydown", function(n, i) {
return i && (e = i), e.input += n ? n.keyCode : event.keyCode, e.input.length > e.pattern.length && (e.input = e.input.substr(e.input.length - e.pattern.length)), e.input == e.pattern ? (e.code(t), e.input = "", n.preventDefault(), !1) : void 0
}, this), this.iphone.load(t)
},
code: function(t) {
window.location = t
},
iphone: {
start_x: 0,
start_y: 0,
stop_x: 0,
stop_y: 0,
tap: !1,
capture: !1,
orig_keys: "",
keys: ["UP", "UP", "DOWN", "DOWN", "LEFT", "RIGHT", "LEFT", "RIGHT", "TAP", "TAP"],
code: function(t) {
e.code(t)
},
load: function(t) {
this.orig_keys = this.keys, e.addEvent(document, "touchmove", function(t) {
if (1 == t.touches.length && 1 == e.iphone.capture) {
var n = t.touches[0];
e.iphone.stop_x = n.pageX, e.iphone.stop_y = n.pageY, e.iphone.tap = !1, e.iphone.capture = !1, e.iphone.check_direction()
}
}), e.addEvent(document, "touchend", function() {
1 == e.iphone.tap && e.iphone.check_direction(t)
}, !1), e.addEvent(document, "touchstart", function(t) {
e.iphone.start_x = t.changedTouches[0].pageX, e.iphone.start_y = t.changedTouches[0].pageY, e.iphone.tap = !0, e.iphone.capture = !0
})
},
check_direction: function(t) {
x_magnitude = Math.abs(this.start_x - this.stop_x), y_magnitude = Math.abs(this.start_y - this.stop_y), x = this.start_x - this.stop_x < 0 ? "RIGHT" : "LEFT", y = this.start_y - this.stop_y < 0 ? "DOWN" : "UP", result = x_magnitude > y_magnitude ? x : y, result = 1 == this.tap ? "TAP" : result, result == this.keys[0] && (this.keys = this.keys.slice(1, this.keys.length)), 0 == this.keys.length && (this.keys = this.orig_keys, this.code(t))
}
}
};
return "string" == typeof t && e.load(t), "function" == typeof t && (e.code = t, e.load()), e
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment