Created
May 25, 2017 19:42
-
-
Save skoch/ded54add57d59026c819350d6d0f6362 to your computer and use it in GitHub Desktop.
Konami code with node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(pattern, callback) { | |
var konami = { | |
addEvent: function(obj, type, fn, refObj) { | |
if (obj.addEventListener) { | |
obj.addEventListener(type, fn, false); | |
} else if (obj.attachEvent) { | |
// IE | |
obj[`e${type}${fn}`] = fn; | |
obj[type + fn] = function() { | |
obj[`e${type}${fn}`](window.event, refObj); | |
}; | |
obj.attachEvent(`on${type}`, obj[type + fn]); | |
} | |
}, | |
input: '', | |
// pattern: '38384040373937396665', | |
pattern: pattern, | |
load: function(link) { | |
this.addEvent(document, 'keydown', function(e, refObj) { | |
if (refObj) konami = refObj; // IE | |
konami.input += e ? e.keyCode : event.keyCode; | |
if (konami.input.length > konami.pattern.length) { | |
const n = konami.input.length - konami.pattern.length; | |
konami.input = konami.input.substr(n); | |
} | |
if (konami.input === konami.pattern) { | |
konami.code(link); | |
konami.input = ''; | |
e.preventDefault(); | |
return false; | |
} | |
return true; | |
}, this); | |
this.iphone.load(link); | |
}, | |
code: function(link) { | |
window.location = link; | |
}, | |
iphone: { | |
start_x: 0, | |
start_y: 0, | |
stop_x: 0, | |
stop_y: 0, | |
tap: false, | |
capture: false, | |
orig_keys: '', | |
keys: ['UP', 'UP', 'DOWN', 'DOWN', 'LEFT', 'RIGHT', 'LEFT', 'RIGHT', 'TAP', 'TAP'], | |
code: function(link) { | |
konami.code(link); | |
}, | |
load: function(link) { | |
this.orig_keys = this.keys; | |
konami.addEvent(document, 'touchmove', function(e) { | |
if (e.touches.length === 1 && konami.iphone.capture === true) { | |
const touch = e.touches[0]; | |
konami.iphone.stop_x = touch.pageX; | |
konami.iphone.stop_y = touch.pageY; | |
konami.iphone.tap = false; | |
konami.iphone.capture = false; | |
konami.iphone.check_direction(); | |
} | |
}); | |
konami.addEvent(document, 'touchend', function(evt) { | |
if (konami.iphone.tap === true) konami.iphone.check_direction(link); | |
}, false); | |
konami.addEvent(document, 'touchstart', function(evt) { | |
konami.iphone.start_x = evt.changedTouches[0].pageX; | |
konami.iphone.start_y = evt.changedTouches[0].pageY; | |
konami.iphone.tap = true; | |
konami.iphone.capture = true; | |
}); | |
}, | |
check_direction: function(link) { | |
const xMagnitude = Math.abs(this.start_x - this.stop_x); | |
const yMagnitude = Math.abs(this.start_y - this.stop_y); | |
const x = ((this.start_x - this.stop_x) < 0) ? 'RIGHT' : 'LEFT'; | |
const y = ((this.start_y - this.stop_y) < 0) ? 'DOWN' : 'UP'; | |
let result = (xMagnitude > yMagnitude) ? x : y; | |
result = (this.tap === true) ? 'TAP' : result; | |
if (result === this.keys[0]) this.keys = this.keys.slice(1, this.keys.length); | |
if (this.keys.length === 0) { | |
this.keys = this.orig_keys; | |
this.code(link); | |
} | |
}, | |
}, | |
}; | |
// typeof callback === "string" && konami.load(callback); | |
if (typeof callback === 'function') { | |
konami.code = callback; | |
konami.load(); | |
} else if (typeof callback === 'string') { | |
konami.load(callback); | |
} | |
return { | |
konami, | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Konami from '../components/konami'; | |
// 4d3d3d3 | |
const tayne = new Konami('52685168516851', function() { | |
console.log('tayne!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment