Repository navigation
Expand file tree
/
Copy pathcontent_script.js
More file actions
62 lines (55 loc) · 2.56 KB
/
Copy pathcontent_script.js
File metadata and controls
62 lines (55 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let buttonPressStates = {};
function triggerClick(selector) {
const element = document.querySelector(selector);
console.log(element);
if (element) {
element.click();
}
}
function handleGamepadInput() {
const gamepad = navigator.getGamepads()[0];
if (!gamepad) return;
// Define your button mappings and associated actions here
const buttonMappings = {
1: () => triggerClick('button[name="chooseMove"][value="1"]'), // A for Attack 1
0: () => triggerClick('button[name="chooseMove"][value="2"]'), // B for Attack 2
3: () => triggerClick('button[name="chooseMove"][value="3"]'), // X for Attack 3
2: () => triggerClick('button[name="chooseMove"][value="4"]'), // Y for Attack 4
4: () => triggerClick('button[data-tooltip="switchpokemon|0"]'), // L for Switch 1
5: () => triggerClick('button[data-tooltip="switchpokemon|1"]'), // R for Switch 2
6: () => triggerClick('button[data-tooltip="switchpokemon|2"]'), // ZL for Switch 3
7: () => triggerClick('button[data-tooltip="switchpokemon|3"]'), // ZR for Switch 4
8: () => triggerClick('button[data-tooltip="switchpokemon|4"]'), // - for Switch 5
9: () => triggerClick('button[data-tooltip="switchpokemon|5"]'), // + for Switch 6
12: () => triggerClick('div.battle-controls > div > div.movecontrols > div.movemenu > label.megaevo'), // Home to check/uncheck mega evo
};
gamepad.buttons.forEach((button, index) => {
if (button.pressed) {
if (!buttonPressStates[index]) { // Check if the button was not previously pressed
const action = buttonMappings[index];
if (action) {
action(); // Execute the action
buttonPressStates[index] = true; // Mark the button as pressed
}
}
} else {
buttonPressStates[index] = false; // Reset the state when the button is released
}
});
}
function startGamepadLoop() {
const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
function loop() {
handleGamepadInput();
requestAnimationFrame(loop);
}
loop();
}
window.addEventListener("gamepadconnected", (event) => {
console.log("Gamepad connected:", event.gamepad.id);
startGamepadLoop();
});
window.addEventListener("gamepaddisconnected", (event) => {
console.log("Gamepad disconnected:", event.gamepad.id);
});