Skip to content

Instantly share code, notes, and snippets.

@batshoes
Last active April 6, 2021 22:14
Show Gist options
  • Save batshoes/7592e7bf1567568fdd1ec7fd5287cfb2 to your computer and use it in GitHub Desktop.
Save batshoes/7592e7bf1567568fdd1ec7fd5287cfb2 to your computer and use it in GitHub Desktop.
Konami Code Easter Egg
// 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