forked from jaimeadf/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
75 lines (60 loc) · 2.3 KB
/
index.jsx
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
import React from 'react';
import { DiscordModules, WebpackModules, Patcher, DiscordContextMenu } from '@zlibrary/api';
import Plugin from '@zlibrary/plugin';
const { StreamStore, StreamPreviewStore, ModalStack } = DiscordModules;
const ImageModal = WebpackModules.getByDisplayName('ImageModal');
const MaskedLink = WebpackModules.getByDisplayName('MaskedLink');
export default class BiggerStreamPreview extends Plugin {
onStart() {
this.patchUserContextMenus();
}
onStop() {
Patcher.unpatchAll();
}
patchUserContextMenus() {
const UserContextMenus = WebpackModules.findAll(m => m?.default?.displayName?.includes('UserContextMenu'));
const patch = (thisObject, [props], returnValue) => {
const { user } = props;
const stream = StreamStore.getStreamForUser(user.id);
if (!stream) return;
const previewURL = StreamPreviewStore.getPreviewURL(stream.guildId, stream.channelId, stream.ownerId);
returnValue.props.children.props.children.push(
DiscordContextMenu.buildMenuItem({
type: 'separator'
}),
DiscordContextMenu.buildMenuItem({
label: 'View Stream Preview',
action: () => this.showImageModal(previewURL),
disabled: previewURL === null
})
);
};
for (const UserContextMenu of UserContextMenus) {
Patcher.after(UserContextMenu, 'default', patch);
}
}
async showImageModal(url) {
const image = await this.fetchImage(url);
ModalStack.push(ImageModal, {
src: url,
placeholder: url,
original: url,
width: image.width,
height: image.height,
onClickUntrusted: e => e.openHref(),
renderLinkComponent: props => <MaskedLink {...props} />
});
}
async fetchImage(url) {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = url;
image.addEventListener('load', () => {
resolve(image);
});
image.addEventListener('error', () => {
reject(new Error('Image not found'));
});
});
}
}