Erinnert ihr euch an den berühmten Konami-Code, der bereits zu Zeiten des Nintendo Entertainment Systems (NES – zum ersten mal in “Gradius”) das virtuelle Leben in diversen Konami-Spiele deutlich einfacher machte?
↑ ↑ ↓ ↓ ← → ← → B A
Der Erfinder des Konami Codes, Kazuhisa Hashimoto, ist im Februar 2020 im Alter von gerade mal 61 Jahren verstorben. In Gedenken anbei ein kleines EasterEgg-Script, mit dem man durch Eingabe von Codes auf seiner eigenen Webseite ein beliebiges Event ausführen kann.
Frameworks wie jQuery werden nicht benötigt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
var PageEasterEgg; (function(document) { PageEasterEgg = { callbacks: {}, positions: {konami: 0}, codes: {konami: ['up', 'up', 'down', 'down', 'left', 'right', 'left', 'right', 'b', 'a']}, /** * @param e * @param type * @private */ _listen: function (e, type) { var inp = (e.key || e.code).toLowerCase().replace(/^(arrow|key)/, ''); if (this.codes[type].indexOf(inp) != -1 && inp == this.codes[type][this.positions[type]]) { this.positions[type]++; if (this.positions[type] == this.codes[type].length) { // console.log("Input for \"" + type.toUpperCase() + "\" is correct."); if (this.callbacks[type] && typeof this.callbacks[type] == "function") { this.callbacks[type](); this.positions[type] = 0; } } } else { this.positions[type] = 0; } }, /** * @param input * @return type * @private */ _addCustom: function (input) { var type = 'custom' + Math.floor(Math.random() * 900000); this.positions[type] = 0; this.codes[type] = []; for (var i = 0; i < input.length; i++) { this.codes[type].push(input[i]); } return type; }, /** * @param type * @param callback */ add: function (type, callback) { if (typeof this.codes[type] == "undefined") { // type is custom code // only alphanumeric characters are allowed // maximum of 64 characters if (typeof type == "string" && /[a-z0-9 ]/g.test(type.toLowerCase())) { var newString = type.toLowerCase().trim().replace(/\s{2,}/, ' '); if (newString.length > 64) { console.error("Error from PageEasterEgg:", "Custom code \"" + type + "\" is too long (Max <= 64 characters)."); return; } type = this._addCustom(newString); } else if (Array.isArray(type)) { if (type.length > 32) { console.error("Error from PageEasterEgg:", "Custom input is too long (Max <= 32 input commands)."); return; } type = this._addCustom(type); } else { return; } } // Possible Todo: Security validation of the callback content if (typeof callback == "function") { this.callbacks[type] = callback; } document.addEventListener("keydown", function (e) { PageEasterEgg._listen(e, type); }); } }; }(document)); // EXAMPLES // The Konami Code PageEasterEgg.add('konami', function () { // Do something }); // Add costum code e.g. doom invulnerability PageEasterEgg.add('iddqd', function () { // Do something }); // Add costume code e.g. doom all weapons and items PageEasterEgg.add('idkfa', function () { // Do something }); // Custom code line PageEasterEgg.add('something something', function () { // Do something }); // Custom code segment: key input sequence PageEasterEgg.add(['up', 'down', 'left', 'right'], function () { // Do something }); |