Konami.js
Konami.js is a JavaScript package that allows web developers to implement an event listener for the Konami code on their web pages. By pressing up, up, down, down, left, right, left, right, B, A, Enter, one or more custom JavaScript functions will execute.
Examples
ES6
Install
-
npm install @gamingmedley/konami.js --save
or yarn add @gamingmedley/konami.js
Adding an Event Listener
import Konami from '@gamingmedley/konami.js';
Konami.add(() => {
alert('This will alert every time the Konami code is entered.');
});
Adding a Single-Execution Event Listener
import Konami from '@gamingmedley/konami.js';
Konami.add((unsubscribe) => {
unsubscribe(); // Unsubscribes the listener from future Konami code entries.
alert('This will only alert the first time the Konami code is entered.');
});
Removing an Event Listener
import Konami from '@gamingmedley/konami.js';
const unsubscribe = Konami.add(() => {
alert('This will not alert, because it will have been removed.');
});
unsubscribe();
Methods
add
Queues a function to be executed when the Konami code is entered by the user.
Returns an unsubscribe function. When executed, the unsubscribe function will disable the associated function from executing in future uses of the Konami code.
Parameters
-
function e
: The function to be executed when the Konami code is entered by the user. An unsubscribe function is passed as a parameter to it.
remove
The function associated with the ID will no longer execute when the Konami code is entered.
Returns true
on success, false
on failure.
Parameters
-
string id
: The ID of the event to remove.