Skip to content

Instantly share code, notes, and snippets.

@sprngr
Created March 24, 2022 02:45
Show Gist options
  • Save sprngr/b708e56fcf20827b46d8505e9411913b to your computer and use it in GitHub Desktop.
Save sprngr/b708e56fcf20827b46d8505e9411913b to your computer and use it in GitHub Desktop.
Tampermonkey script to map gamepad buttons to Ludum Dare theme slaughter inputs
// ==UserScript==
// @name Slaughterpad - Ludum Dare theme slaughter controller mapping
// @version 0.1
// @description Rip and tear until it is done
// @author sprngr
// @match https://ldjam.com/events/ludum-dare/50/theme
// @icon https://www.google.com/s2/favicons?sz=64&domain=ldjam.com
// @grant none
// ==/UserScript==
(function () {
'use strict';
// global gamepad object
let gamepadIndex;
window.addEventListener('gamepadconnected', (event) => {
gamepadIndex = event.gamepad.index;
});
setInterval(() => {
if (gamepadIndex !== undefined) {
// a gamepad is connected and has an index
const myGamepad = navigator.getGamepads()[gamepadIndex];
myGamepad.buttons.map(e => e.pressed).forEach((isPressed, buttonIndex) => {
if (isPressed) {
console.log(`Button ${buttonIndex} is pressed`);
let theme = document.querySelector('.-theme').innerText;
switch (buttonIndex) {
case 0:
document.querySelector('.ui-button.middle.big.-yes').click();
console.log(`Voted Yes on ${theme}`);
break;
case 2:
document.querySelector('.ui-button.middle.big.-no').click();
console.log(`Voted No on ${theme}`);
break;
case 1:
document.querySelector('.ui-button.-flag').click();
console.log(`Flagged ${theme}`);
break;
default:
console.log("Mash more buttons...");
}
}
})
}
}, 50)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment