Last active
April 6, 2021 22:14
-
-
Save batshoes/7592e7bf1567568fdd1ec7fd5287cfb2 to your computer and use it in GitHub Desktop.
Konami Code Easter Egg
This file contains 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
// Insert this into your already sourced JS file, or link to it in the HEAD of your DOM. | |
// Add whatever you like to the keystroke keychain match. | |
document.addEventListener('DOMContentLoaded', () => { | |
'use strict'; | |
// Standard Konami Code keystroke chain. | |
// Up Up Down Down Left Right Left Right B A Start | |
konamify("arrowuparrowuparrowdownarrowdownarrowleftarrowrightarrowleftarrowrightbaenter"); | |
}); | |
function konamify(code) { | |
let buffer = []; | |
let lastKeyTime = Date.now(); | |
document.addEventListener('keydown', event => { | |
const key = event.key.toLowerCase(); | |
const currentTime = Date.now(); | |
if (currentTime - lastKeyTime > 1000) { | |
buffer = []; | |
} | |
lastKeyTime = currentTime; | |
buffer.push(key); | |
if (buffer.join('') === code) { | |
// Konamified. | |
console.log('Konamified'); | |
// Your Konami Code action here. | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment