-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
71 lines (62 loc) · 2.19 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 @mzyy94
// @ts-check
import { Controller } from "./controller.js";
/**
* Apply preview color to svg element class
*
* @param {HTMLObjectElement} object
* @param {Controller} controller
*/
const previewColor = (object, controller) => {
const sheet = object.contentDocument?.querySelector("style")?.sheet;
if (!sheet) return;
/**
* @param {string} selector
* @param {string} color
*/
const replaceStyle = (selector, color) => {
const index = Array.from(sheet.rules).findIndex(
(rule) => rule.selectorText == selector
);
sheet.insertRule(`${selector} { fill: ${color} }`, index + 1);
sheet.deleteRule(index);
};
replaceStyle(".body-shell", controller.bodyColor);
if (controller.type == "procon") {
replaceStyle(".left-grip", controller.leftGripColor);
replaceStyle(".right-grip", controller.rightGripColor);
}
replaceStyle(".button", controller.buttonColor);
};
/**
* Set battery capacity level of battery icon
*
* @param {HTMLObjectElement} object
* @param {number} voltage
*/
const setBatteryCapacity = (object, voltage) => {
const level = (voltage - 3.3) / (4.2 - 3.3);
const capacity = object.contentDocument?.querySelector("#capacity");
capacity?.setAttribute("width", String(416 * level));
};
const connectController = () =>
navigator.hid?.requestDevice({ filters: [{ vendorId: 0x057e }] })
.then(async (devices) => {
if (devices.length == 1) {
const device = devices[0];
await device.close();
await device.open();
console.log(device.productName, "connected");
const controller = new Controller(device);
await controller.startConnection();
await controller.fetchDeviceInfo();
console.table(controller);
return controller;
} else if (devices.length > 1) {
return Promise.reject("Please select only one controller.");
}
})
?? Promise.reject("unavailable");
const presetColors = ["#828282", "#0AB9E6", "#FF3C28", "#E6FF00", "#1EDC00", "#FF3278", "#E10F00", "#4655F5", "#B400E6", "#FAA005", "#FFFFFF"];
Object.assign(window, { previewColor, setBatteryCapacity, connectController, presetColors });