-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (146 loc) · 5.11 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
import { Plugin } from "@vizality/entities";
import {
getModule,
contextMenu,
getModuleByDisplayName,
} from "@vizality/webpack";
import { patch, unpatch } from "@vizality/patcher";
import React, { useRef } from "react";
import { joinClassNames } from "@vizality/util/dom";
import { AsyncComponent } from "@vizality/components";
import { sleep } from "@vizality/util/time";
import { getOwnerInstance, findInTree } from "@vizality/util/react";
import { waitForElement } from "@vizality/util/dom";
import { shell } from "electron";
import ContextMenu from "./components/ContextMenu";
const PanelSubtext = AsyncComponent.fromDisplayName("PanelSubtext");
const Tooltip = AsyncComponent.fromDisplayName("Tooltip");
const HeaderBarButton = require("./components/HeaderBarButton");
const audio = require("./functions/audio");
const RADIO_DEFAULT_IMAGE = "https://i.imgur.com/nWLt7eE.jpg";
export default class vzradio extends Plugin {
async start() {
this.injectStyles("./style.scss");
audio.setup(this.settings.get, this.settings.set);
const HeaderBarContainer = await getModuleByDisplayName(
"HeaderBarContainer"
);
const HeaderBar = await getModuleByDisplayName("HeaderBar");
patch(
"radio-header-bar",
HeaderBarContainer.prototype,
"render",
(args, res) => {
res.props.toolbar.props.children.push(
<HeaderBarButton settings={this.settings} bartype={HeaderBar.Icon} />
);
return res;
}
);
this.settings.set("vz-radio", false);
this._injectPlayer();
}
stop() {
audio.stop();
unpatch("radio-header-bar");
unpatch("vz-radio-player");
}
render(base) {
const radio = require("./functions/radio.json");
const radioJSON = require("./functions/radio.json");
const { avatar, avatarWrapper } = getModule(
"container",
"usernameContainer"
);
radio.name = this.settings.get("advanced-settings-override", false)
? this.settings.get("radio-name-override", radioJSON.name)
: radioJSON.name;
radio.stream = this.settings.get("advanced-settings-override", false)
? this.settings.get("radio-stream-override", radioJSON.stream)
: radioJSON.stream;
radio.homepage = this.settings.get("advanced-settings-override", false)
? this.settings.get("radio-homepage-override", radioJSON.homepage)
: radioJSON.homepage;
radio.description = this.settings.get("advanced-settings-override", false)
? this.settings.get("radio-category-override", radioJSON.description)
: radioJSON.description;
radio.favicon = this.settings.get("advanced-settings-override", false)
? this.settings.get("radio-image-override", radioJSON.favicon)
: radioJSON.favicon;
return {
...base,
props: {
...base.props,
onMouseEnter: () => void 0,
onMouseLeave: () => void 0,
onContextMenu: (e) => {
contextMenu.openContextMenu(e, () => {
return (
<ContextMenu set={this.settings.set} get={this.settings.get} />
);
});
},
className: base.props.className,
children: [
<div className="vz-radio-player-inner">
<div
className={joinClassNames(
"vz-radio-player-album-cover-wrapper",
avatarWrapper
)}
onClick={() => {
shell.openExternal(radio.homepage);
}}
>
<Tooltip text={radio.name}>
{(props) => (
// Not using LazyImage here because it seems to break the tooltip
<img
{...props}
src={radio.favicon || RADIO_DEFAULT_IMAGE}
className={joinClassNames(
"vz-radio-player-album-cover",
avatar
)}
onError={(e) => {
e.target.src = RADIO_DEFAULT_IMAGE;
}}
width="32"
height="32"
/>
)}
</Tooltip>
</div>
<div className="vz-radio-player-metadata">
<div className="radio-name">{radio.name}</div>
<PanelSubtext className="radio-artist">
Category: {radio.description}
</PanelSubtext>
</div>
</div>,
],
},
};
}
async _injectPlayer() {
await sleep(1e3); // It ain't stupid if it works
const { container } = getModule("container", "usernameContainer");
const accountContainer = await waitForElement(`section > .${container}`);
const instance = getOwnerInstance(accountContainer);
await patch(
"spotify-in-discord-player",
instance.__proto__,
"render",
(_, res) => {
const base = findInTree(res, (t) => t.props?.className === container);
if (this.settings.get("vz-radio", false))
return [
res,
<div className="vz-radio-player">{this.render(base)}</div>,
];
else return res;
}
);
instance.forceUpdate();
}
}