-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVKControls.plugin.js
More file actions
250 lines (239 loc) · 8.97 KB
/
Copy pathVKControls.plugin.js
File metadata and controls
250 lines (239 loc) · 8.97 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @name VKControls
* @author SPRAVEDLIVO
* @description Connect VK music player with discord
* @version 0.0.1
* @authorId 355826920270594058
* @website https://spravedlivo.dev/vkreact
* @source https://github.com/VkReact/VKControls/VKControls.plugin.js
*/
const fs = require('fs')
const https = require('https')
let fetch = (url) => new Promise((resolve, reject) => {
let data = ''
https.get(url, {
headers: {
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36"
}
}, (res) => {
res.on('data', (d) => {
data += d.toString()
});
res.on('end', () => {
resolve(data)
})
res.on('error', (e) => reject(e))
})
})
let download = (url, dest) => new Promise(async (resolve, reject) => {
let file = fs.createWriteStream(dest);
https.get(url, function (response) {
response.pipe(file)
file.on('finish', function () {
file.close(resolve)
})
}).on('error', function (err) {
fs.unlink(dest)
reject(err.message)
})
})
async function resolveTree() {
let tree = await fetch("https://api.github.com/repos/websockets/ws/git/trees/master?recursive=true")
tree = JSON.parse(tree)
let final = {}
tree["tree"].forEach(it => {
final[it["path"]] = it
})
return final
}
class PanelController {
createPanel() {
let panel = document.querySelector(".panels-3wFtMD")
let cloned = panel.cloneNode(true)
Array.from(cloned.querySelector(".container-YkUktl").children).forEach(it => it.remove())
cloned.querySelector(".container-YkUktl").innerHTML = `
<div id="vkc_im">
<img class="vkc_im_img">
</div>
<div id="vkc_grid">
<div class="size14-3fJ-ot title-338goq vkc"><span></span>
<div class="size14-3fJ-ot title-338goq vkc artist">
</div>
</div>
<div class="vkc_button prev_button" onclick="vkc.prev()">
<img class="vkc_im_img btn" src="https://img.icons8.com/ios-filled/100/ffffff/rewind.png"></img>
</div>
<div class="vkc_button pause_button" onclick="vkc.playpause()">
<img class="vkc_im_img btn pause" src="https://img.icons8.com/material-outlined/32/ffffff/pause--v1.png">
</div>
<div class="vkc_button next_button" onclick="vkc.next()">
<img class="vkc_im_img btn" src="https://img.icons8.com/ios-filled/100/ffffff/fast-forward.png"></img>
</div>
<div class="vkc_name_and_pg">
<div class="progress-container" onclick="vkc.pg(event, this)">
<div class="progress">
</div>
</div>
</div>
</div>
`
cloned.id = "vkc_panel"
this.node = cloned
panel.before(cloned)
}
constructor() {
this.createPanel()
}
stop() {
this.node.remove()
}
urlSwitch() {
let nextChild = this.node.parentElement.children[Array.from(this.node.parentElement.children).indexOf(this.node) + 1]
if (nextChild && nextChild.tagName == "NAV") {
nextChild.after(this.node)
}
}
}
let jsonify = JSON.stringify
let mapRange = function (from, to, value) {
if (parseFloat(value) < parseFloat(from[0])) return parseFloat(to[0]);
else if (parseFloat(value) > parseFloat(from[1])) return parseFloat(to[1]);
else return parseFloat(to[0]) + (parseFloat(value) - parseFloat(from[0])) * (parseFloat(to[1]) - parseFloat(to[0])) / (parseFloat(from[1]) - parseFloat(from[0]));
};
let stateButtons = { "PLAY": "https://img.icons8.com/material-outlined/32/ffffff/pause--v1.png", "PAUSE": "https://img.icons8.com/ios-glyphs/30/ffffff/play--v1.png" }
module.exports = class VKControls {
prev() {
this.wss.send_latest(jsonify({ "command": "playPrev" }))
}
next() {
this.wss.send_latest(jsonify({ "command": "playNext" }))
}
playpause() {
this.wss.send_latest(jsonify({ "command": "play_pause" }))
}
pg(e, element) {
let rects = element.getClientRects()[0]
if (!this.max_time) return
let pos = Math.round(mapRange([rects.left, rects.left + rects.width], [0, this.max_time], e.clientX))
this.wss.send_latest(jsonify({ "command": "seek_time", "time": pos }))
}
async load() {
window.vkc = this
let pluginFolder = BdApi.Plugins.folder
if (!fs.existsSync(`${pluginFolder}/ws-master`)) {
console.log("WS not found. Executing step 1: resolving tree")
let tree = await resolveTree()
console.log("Step 2: creating folder")
fs.mkdirSync(`${pluginFolder}/ws-master`)
console.log("Step 3: downloading repo (v1.0)")
for (const [path, obj] of Object.entries(tree)) {
if (obj.type == "tree") {
fs.mkdirSync(`${pluginFolder}/ws-master/${path}`)
}
else {
await download(`https://raw.githubusercontent.com/websockets/ws/master/${path}`, `${pluginFolder}/ws-master/${path}`)
}
}
console.log("downloading complete")
}
else {
console.log("detected ws installation")
try {
require(`${pluginFolder}/ws-master/index.js`)
}
catch (e) {
console.log(`WS Require Error: ${e}`)
return
}
}
this.ws = require(`${pluginFolder}/ws-master/index.js`)
}
se(htmlString) {
var div = document.createElement('div');
div.innerHTML = htmlString.trim();
return div.firstChild;
}
start() {
this.wss = new this.ws.Server({ "port": 6374 })
this.wss.on("connection", wsClient => {
wsClient.send(jsonify({ "command": "request_data" }))
wsClient.on("message", message => {
let parsed = JSON.parse(message)
switch (parsed.command) {
case "update_info": {
if (parsed.status != "NO_PLAYER") {
this.last_sender = wsClient
this.controller.node.querySelector(".size14-3fJ-ot.title-338goq.vkc > span").textContent = parsed.name
this.controller.node.querySelector(".size14-3fJ-ot.title-338goq.vkc.artist").innerText = parsed.artist
this.controller.node.querySelector(".vkc_im_img").src = parsed.cover
this.controller.node.querySelector(".vkc_im_img.btn.pause").src = stateButtons[parsed.status]
this.controller.node.querySelector(".progress").style.width = `${parsed.progress}%`
this.max_time = parsed.duration
this.controller.node.style.height = '53px'
}
break
}
}
})
})
this.wss.send_latest = (message) => {
if (this.last_sender) this.last_sender.send(message)
}
BdApi.injectCSS("vkc_css", `
.vkc_button {
align-self: center;
}
#vkc_panel {
transition: height 1s;
height: 1px;
}
#vkc_grid {
display:grid;
grid-template-columns: 9rem repeat(3, 16px);
}
.progress-container {
height: 0.4rem;
width: 11.5rem;
border-radius: 0.4rem;
background: white;
}
#vkc_im {
margin-right: 8px;
}
.size14-3fJ-ot.title-338goq.vkc {
padding-bottom: 10px;
word-break: break-word;
height: 32px;
}
.size14-3fJ-ot.title-338goq.vkc.artist {
color: var(--header-secondary);
}
.vkc_im_img {
height: 32px;
width: 32px;
border-radius: 4px;
background-color: rgba(0,0,0,0);
}
.vkc_im_img.btn {
height: 16px;
width: 16px;
}
.progress-container .progress {
height: 100%;
width: 0;
border-radius: 0.4rem;
background: #ff4754;
transition: width 0.4s ease;
}
`)
this.controller = new PanelController()
}
stop() {
this.wss.close()
BdApi.clearCSS("vkc_css")
this.controller.stop()
}
onSwitch() {
this.controller.urlSwitch()
}
}