-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
283 lines (255 loc) · 10.1 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { React, getModule, spotify, getModuleByDisplayName, getModulesByKeyword } from '@vizality/webpack';
import { getOwnerInstance, findInTree } from '@vizality/util/react';
import { waitForElement } from '@vizality/util/dom';
import { patch, unpatch } from '@vizality/patcher';
import { Plugin } from '@vizality/entities';
import { sleep } from '@vizality/util/time';
import playerStoreActions from './stores/player/actions';
import { SPOTIFY_DEFAULT_IMAGE } from './constants';
import playerStore from './stores/player/store';
import Settings from './components/Settings';
import Player from './components/Player';
import SpotifyAPI from './SpotifyAPI';
import * as commands from './commands';
import * as i18n from './i18n';
export default class SpotifyInDiscord extends Plugin {
constructor () {
super();
this._handleSpotifyData = this._handleSpotifyData.bind(this);
this.ponged = false;
this.position = this.settings.get('player-position', 'channel-list')
this.minimalist = this.settings.get('minimalist', false);
// hacky workaround to get the websocket
this.originalSend = WebSocket.prototype.send;
this.socket = null;
const _this = this;
this.window = {};
this.window.sockets = [];
WebSocket.prototype.send = function(...args) {
if (_this.window.sockets.indexOf(this) === -1) {
_this.window.sockets.push(this);
if (this.url.startsWith("wss://dealer.spotify.com")) {
_this._webSocketPatch();
}
}
return _this.originalSend.call(this, ...args);
};
}
async start () {
vizality.api.i18n.injectAllStrings(i18n);
this.injectStyles('styles/main.scss');
document.body.style.setProperty('--spotify-in-discord__player-album-border-radius', `${this.settings.get('coverRoundness', 50)}%`);
this._injectPlayer();
this._patchAutoPause();
await SpotifyAPI.getAccessToken();
playerStoreActions.fetchDevices()
?.catch(() => null);
this.registerSettings(props => <Settings addonId={this.addonId} patch={this._patchAutoPause.bind(this)} reinject={this._reinjectPlayer.bind(this)} {...props} />);
commands.registerCommands();
await sleep(10000);
}
stop () {
unpatch('spotify-in-discord-player');
this._patchAutoPause(true);
WebSocket.prototype.send = this.originalSend;
if (this.socket) this.socket.removeEventListener('message', this._handleSpotifyData);
vizality.api.commands.unregisterCommand('spotify');
const { container } = getModule('container', 'usernameContainer');
const accountContainer = document.querySelector(`section > .${container}`);
const instance = getOwnerInstance(accountContainer);
instance.forceUpdate();
}
async openPremiumDialog () {
const PremiumDialog = getModuleByDisplayName('SpotifyPremiumUpgrade');
const { openModal } = getModule('openModal', 'closeModal');
openModal(props => <PremiumDialog {...props} />);
}
async _injectPlayer () {
await sleep(1e3); // It ain't stupid if it works
this.position = this.settings.get('player-position', 'channel-list')
this.minimalist = this.settings.get('minimalist', false);
this.coverCursor = this.settings.get('cover-cursor', false);
this.spinningAlbumCover = this.settings.get('spinning-album-cover', false);
const { container } = getModule('container', 'usernameContainer');
const accountContainer = await waitForElement(`section > .${container}`);
const instance = getOwnerInstance(accountContainer);
await patch('spotify-in-discord-player-base', instance.__proto__, 'render', (_, res) => {
this.realRes = findInTree(res, t => t.props?.className === container);
return [res];
});
instance.forceUpdate();
unpatch('spotify-in-discord-player-base')
const baseExports = {
addonId: this.addonId,
base: this.realRes,
getSetting: this.settings.get,
updateSetting: this.settings.set,
minimalist: this.minimalist,
coverCursor: this.coverCursor,
spinningAlbumCover: this.spinningAlbumCover,
}
if (this.position === 'channel-list') {
await patch('spotify-in-discord-player', instance.__proto__, 'render', (_, res) => {
return [<Player {...baseExports} reinject={this._reinjectPlayer.bind(this)} />, res];
});
instance.forceUpdate();
} else if (this.position === 'member-list-top' || this.position === 'member-list-bottom') {
const { ListThin } = getModule(["ListThin"], false);
patch('spotify-in-discord-player', ListThin, "render", (args, res) => {
if (
!args[0] ||
!args[0]["data-list-id"] ||
!args[0]["data-list-id"].startsWith("members")
) {
return res;
}
res.props.children = this.position == "member-list-bottom" ? [
res.props.children, <Player {...baseExports} reinject={this._reinjectPlayer.bind(this)} extraClasses={["bottom"]} />,
] : [
<Player {...baseExports} reinject={this._reinjectPlayer.bind(this)} extraClasses={["top"]}/>,
res.props.children,
];
return res;
});
}
}
async _reinjectPlayer () {
unpatch('spotify-in-discord-player');
if (this.position === 'channel-list') {
const { container } = getModule('container', 'usernameContainer');
const accountContainer = await waitForElement(`section > .${container}`);
const instance = getOwnerInstance(accountContainer);
await patch('spotify-in-discord-player-remover', instance.__proto__, 'render', (_, res) => {
res = res.filter(
(item) => item?.props?.addonId !== this.addonId
)
return [res];
});
instance.forceUpdate();
} else if (this.position === 'member-list-top' || this.position === 'member-list-bottom') {
const { ListThin } = getModule(["ListThin"], false);
await patch('spotify-in-discord-player-remover', ListThin, "render", (args, res) => {
if (
!args[0] ||
!args[0]["data-list-id"] ||
!args[0]["data-list-id"].startsWith("members")
) {
return res;
}
res.props.children = res.props.children.filter(
(item) => item?.props?.addonId != this.addonId
);
return res;
});
}
unpatch('spotify-in-discord-player-remover');
this._injectPlayer()
}
_patchAutoPause (revert) {
if (this.settings.get('noAutoPause', true)) {
const spotifyMdl = getModule('initialize', 'wasAutoPaused');
if (revert) {
spotifyMdl.wasAutoPaused = spotifyMdl._wasAutoPaused;
spotify.pause = spotify._pause;
} else {
spotifyMdl._wasAutoPaused = spotifyMdl.wasAutoPaused;
spotifyMdl.wasAutoPaused = () => false;
spotify._pause = spotify.pause;
spotify.pause = () => void 0;
}
}
}
_webSocketPatch() {
const _this = this;
this.socket = this.window.sockets?.filter(socket => socket.url.startsWith("wss://dealer.spotify.com"))[0];
if (this.socket) {
this.socket.addEventListener("message", this._handleSpotifyData);
WebSocket.prototype.send = this.originalSend;
}
}
_handleSpotifyData(data) {
const parsedData = JSON.parse(data.data);
if (parsedData.type === "pong") {
if (!this.ponged) {
this.ponged = true;
SpotifyAPI.getPlayer()
?.then(player => this._handlePlayerState(player))
?.catch(() => null);
}
}
if (!parsedData.type === 'message' || !parsedData.payloads) return;
if (parsedData.uri === 'wss://event') {
for (const payload of parsedData.payloads || []) {
for (const event of payload.events || []) {
this._handleSpotifyEvent(event);
}
}
}
}
_handleSpotifyEvent (evt) {
switch (evt.type) {
case 'PLAYER_STATE_CHANGED':
this._handlePlayerState(evt.event.state);
break;
case 'DEVICE_STATE_CHANGED':
playerStoreActions.fetchDevices();
break;
}
}
_handlePlayerState (state) {
if (!state?.timestamp) return;
// Handle track
const currentTrack = playerStore.getCurrentTrack();
if ((state.item?.id || state.item?.is_local) && (!currentTrack || currentTrack.id !== state.item?.id || currentTrack.name !== state.item?.name)) {
const { theme } = getModulesByKeyword("renderEmbeds")[0];
const cover =
state.item.album && state.item.album.images[0]
? state.item.album.images[0].url
: `vz-plugin://spotify-in-discord/assets/placeholder-${theme}.png`;
playerStoreActions.updateCurrentTrack({
id: state.item.id,
uri: state.item.uri,
name: state.item.name,
isLocal: state.item.is_local,
duration: state.item.duration_ms,
explicit: state.item.explicit,
cover,
artists: state.item.artists.map(a => a.name).join(', ') || 'Unknown',
album: state.item.album ? state.item.album.name : null,
urls: {
track: state.item.external_urls.spotify,
album: state.item.album ? state.item.album.external_urls.spotify : null
}
});
} else if (state?.currently_playing_type === 'ad') {
playerStoreActions.updateCurrentTrack({
id: null,
uri: null,
name: 'Advertisement',
isLocal: true,
duration: 0,
explicit: null,
cover: SPOTIFY_DEFAULT_IMAGE,
artists: null,
album: null,
urls: null
});
}
// Handle state
playerStoreActions.updatePlayerState({
repeat: state.repeat_state === 'track'
? playerStore.RepeatState.REPEAT_TRACK
: state.repeat_state === 'context'
? playerStore.RepeatState.REPEAT_CONTEXT
: playerStore.RepeatState.NO_REPEAT,
shuffle: state.shuffle_state,
canRepeat: !state.actions.disallows.toggling_repeat_context,
canRepeatOne: !state.actions.disallows.toggling_repeat_track,
canShuffle: !state.actions.disallows.toggling_shuffle,
spotifyRecordedProgress: state.progress_ms,
currentlyPlayingType: state.currently_playing_type,
playing: state.is_playing,
volume: state.device.volume_percent
});
}
}