Created
September 25, 2023 14:06
-
-
Save redphx/5ee2d26848b9b8da36bd423fff8d098b to your computer and use it in GitHub Desktop.
BAYX to ABXY Userscript
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
// ==UserScript== | |
// @name Gamepad buttons swapper | |
// @namespace https://github.com/redphx | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author redphx | |
// @match https://hardwaretester.com/gamepad | |
// @match https://www.xbox.com/*/play* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
function cloneGamepad(gamepad) { | |
const cloned = { | |
id: gamepad.id, | |
index: gamepad.index, | |
connected: gamepad.connected, | |
timestamp: 0, | |
mapping: gamepad.mapping, | |
haptichapticActuators: gamepad.hapticActuators, | |
buttons: [], | |
}; | |
return cloned; | |
} | |
function cloneButtons(buttons) { | |
const tmp = []; | |
buttons.forEach(button => { | |
tmp.push({ | |
pressed: button.pressed, | |
// touched: button.touched, | |
value: button.value, | |
}); | |
}); | |
return tmp; | |
} | |
function swapButton(buttons, indexA, indexB) { | |
const tmp = buttons[indexA]; | |
buttons[indexA] = buttons[indexB]; | |
buttons[indexB] = tmp; | |
return buttons; | |
} | |
let emulatedGamepads = [null, null, null, null]; | |
window.addEventListener('gamepadconnected', e => { | |
const gamepad = e.gamepad; | |
if (gamepad.id.toLowerCase().includes('8bitdo')) { | |
emulatedGamepads[gamepad.index] = cloneGamepad(gamepad); | |
} | |
}); | |
window.addEventListener('gamepaddisconnected', e => { | |
const gamepad = e.gamepad; | |
if (gamepad.id.toLowerCase().includes('8bitdo')) { | |
emulatedGamepads[gamepad.index] = null; | |
} | |
}); | |
const originalGetGamepads = navigator.getGamepads; | |
navigator.getGamepads = function() { | |
const originalGamepads = originalGetGamepads.apply(this); | |
emulatedGamepads.forEach((emulated, index) => { | |
if (!emulated) { | |
return; | |
} | |
const orgGamepad = originalGamepads[index]; | |
let buttons = cloneButtons(orgGamepad.buttons); | |
buttons = swapButton(buttons, 0, 1); | |
buttons = swapButton(buttons, 2, 3); | |
emulated.buttons = buttons; | |
emulated.axes = orgGamepad.axes; | |
emulated.timestamp = orgGamepad.timestamp; | |
originalGamepads[emulated.index] = emulated; | |
}); | |
return originalGamepads; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment