Skip to content

Instantly share code, notes, and snippets.

@AyrA
Created July 8, 2018 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AyrA/2c24609f617c1a887504b9a0473d3d01 to your computer and use it in GitHub Desktop.
Save AyrA/2c24609f617c1a887504b9a0473d3d01 to your computer and use it in GitHub Desktop.
JS Konami code script
//Small konami code script that doesn't uses the usual method of an integer array
//Either run this through a minifier or remove the comments,
//otherwise people can just search for "konami" or variable name combinations
//The string of the konami code itself is almost impossible to find by using search engines.
//Params:
//cb - required(function), called on complete sequence with all 3 parameters
//check - optional(function), checks if key presses should be evaluated, called with all 3 parameters.
//single - optional(bool), if set to something that evaluates to 'true' the secret can only be used once.
(function (cb, check, single) {
var d = "&&((%'%'BA";
var i = 0;
if (typeof(cb) === typeof(function () {})) {
document.addEventListener("keydown", function (e) {
if (typeof(check) !== typeof(cb) || check(cb, check, single)) {
if (i < d.length && d.charCodeAt(i) === e.keyCode) {
if (++i === d.length) {
i = 0;
if (single) {
d = "";
}
cb(cb, check, single);
}
} else {
i = 0;
}
}
});
}
})(/*PARAMS HERE*/);
//Example of a single use alert box that can only be triggered
//if the document title has been set to a certain value.
//Param 1:
//window.alert.bind(window, "You found it")
//Param 2:
//function () {return document.title === "SECRET";}
//Param 3:
//true
// -- /u/AyrA_ch
//Licensed under MIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment