Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
TheLazySquid
GitHub Repository: TheLazySquid/GimkitCheat
Path: blob/main/src/utils.ts
8160 views
1
import { ChangePacket, IDeviceChange, IGimkitWindow } from "./types"
2
3
export function findMatchingParent(node: Element, selector: string) {
4
if (node.matches(selector)) {
5
return node
6
}
7
if (node.parentElement) {
8
return findMatchingParent(node.parentElement, selector)
9
}
10
return null
11
}
12
13
export function parseRGBA(string: string) {
14
let [r, g, b, a] = string
15
.replace('rgba(', '')
16
.replace(')', '')
17
.split(',')
18
.map(value => parseFloat(value.trim()))
19
return { r, g, b, a }
20
}
21
22
export function parseHex(string: string) {
23
let [r, g, b] = string
24
.replace('#', '')
25
.match(/.{1,2}/g)!
26
.map(value => parseInt(value, 16))
27
return { r, g, b }
28
}
29
30
export function rgbToHex(r: number, g: number, b: number) {
31
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b)
32
}
33
34
function componentToHex(c: number) {
35
var hex = Math.round(c).toString(16)
36
return hex.length == 1 ? "0" + hex : hex
37
}
38
39
export function getUnsafeWindow(): IGimkitWindow {
40
if (typeof unsafeWindow === 'undefined') {
41
return window as unknown as IGimkitWindow
42
}
43
return unsafeWindow as unknown as IGimkitWindow
44
}
45
46
export function parseChangePacket(packet: ChangePacket) {
47
let returnVar: IDeviceChange[] = []
48
49
for(let change of packet.changes) {
50
let data: { [index: string]: any } = {}
51
52
let keys = change[1].map((index: number) => packet.values[index])
53
for(let i = 0; i < keys.length; i++) {
54
data[keys[i]] = change[2][i]
55
}
56
57
returnVar.push({
58
id: change[0],
59
data
60
})
61
}
62
63
return returnVar;
64
}
65