-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext.js
More file actions
257 lines (245 loc) · 7.25 KB
/
context.js
File metadata and controls
257 lines (245 loc) · 7.25 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
251
252
253
254
255
256
257
import eventManager from 'src/utils/eventManager.js';
import * as settings from 'src/utils/settings/index.js';
import { global } from 'src/utils/global.js';
import style from 'src/utils/style.js';
import { toast as simpleToast, infoToast } from 'src/utils/2.toasts.js';
import each from 'src/utils/each.js';
import * as fnUser from 'src/utils/user.js';
import ignoreUser from 'src/utils/ignoreUser.js';
import decode from 'src/utils/decode.js';
import { buttonCSS, window } from 'src/utils/1.variables.js';
import Translation from 'src/structures/constants/translation.ts';
const setting = settings.register({
name: Translation.Setting('chat.rightclick'),
key: 'underscript.disable.chatContext',
page: 'Chat',
});
style.add(`
.chatContext {
background-color: #F4F4F4;
margin: 10px;
color: #333;
border: 1px dashed #000;
position: absolute;
z-index: 20;
text-align: center;
border-radius: 10px;
}
.chatContext header {
padding: 0 5px;
height: auto;
}
/* TODO: Why is this !important? */
.chatContext select {
background-color: transparent !important;
}
.chatContext li {
list-style: none;
margin: 0;
padding: 3px;
border-top: 1px solid #CCC;
cursor: pointer;
}
.chatContext .disabled {
background-color: #CCC;
cursor: not-allowed;
}
.chatContext li:not(.disabled):hover {
background-color: #036;
color: #F2F2F2;
}
.chatContext > :last-child {
border-radius: 0 0 10px 10px;
}
`);
let toast;
eventManager.on('jQuery', () => {
const ignorePrefix = 'underscript.ignore.';
const context = (() => {
const container = $('<div class="chatContext">');
const profile = $('<li>Profile</li>');
const message = $('<li>Message</li>');
const ignore = $('<li>Ignore</li>');
const mention = $('<li>Mention</li>');
const mute = $('<li>Mute</li>');
const muteTime = $('<select>');
const header = $('<header>');
eventManager.on('underscript:ready', () => {
each({
profile,
message,
ignore,
mention,
mute,
}, (value, key) => {
value.text(Translation.General(key));
});
});
container.append(header, profile, mention, ignore).hide();
$('body').append(container);
const times = {
1: '1s',
60: '1m',
600: '10m',
3600: '1h',
21600: '6h',
43200: '12h',
86400: '1d',
};
each(times, (item, key) => {
muteTime.append($(`<option value="${key}"${key === '3600' ? ' selected' : ''}>${item}</option>`));
});
mute.append(' ', muteTime);
function open(event) {
if (event.ctrlKey || setting.value()) return;
if (toast) {
toast.close('opened');
}
if (settings.value('underscript.disable.ignorechat')) {
ignore.detach();
} else {
mention.after(ignore);
}
close();
const { id, name, staff, mod } = event.data;
const selfId = global('selfId');
const selfMod = selfId !== id && global('selfMainGroup').priority <= 4;
if (selfMod || global('isFriend')(id)) {
profile.before(message);
} else {
message.detach();
}
event.preventDefault();
if (selfMod) { // Add here to prevent a bug from happening
container.append(mute);
}
// get top/left coordinates
header.html(name);
let left = event.pageX;
const containerWidth = container.outerWidth(true);
if (left + containerWidth > window.innerWidth) {
left -= containerWidth;
}
container.css({
top: `${event.pageY}px`,
left: `${left}px`,
});
container.show();
const disabled = staff || id === selfId;
const muteDisabled = mod || id === selfId;
container.on('click.script.chatContext', 'li', (e) => {
if (!$(e.target).is('li')) {
return;
}
if (e.target === profile[0]) {
global('getInfo')(event.target);
} else if (e.target === mention[0]) {
const input = $(event.target).closest('.chat-box').find('.chat-text');
let text = input.val();
if (text.length !== 0 && text[text.length - 1] !== ' ') {
text += ' ';
}
text += `@${decode(name)} `;
input.val(text).focus();
} else if (e.target === ignore[0]) {
if (disabled) return; // If it's disabled it's disabled...
const key = `${ignorePrefix}${id}`;
if (!settings.value(key)) {
simpleToast({
text: Translation.IGNORED.translate(name),
css: {
'background-color': 'rgba(208, 0, 0, 0.6)',
},
buttons: [{
css: buttonCSS,
text: Translation.UNDO,
className: 'dismiss',
onclick: () => {
settings.remove(key);
updateIgnoreText(id);
},
}],
className: 'dismissable',
});
ignoreUser(name, key, true);
} else {
settings.remove(key);
}
updateIgnoreText(id);
} else if (e.target === mute[0]) {
if (muteDisabled) return;
global('timeout')(`${id}`, muteTime.val());
} else if (e.target === message[0]) {
global('openPrivateRoom')(id, name);
}
close();
});
if (disabled) {
ignore.addClass('disabled');
} else {
ignore.removeClass('disabled');
}
if (muteDisabled) {
mute.addClass('disabled');
muteTime.prop('disabled', true);
} else {
mute.removeClass('disabled');
muteTime.prop('disabled', false);
}
updateIgnoreText(id);
$('html').on('mousedown.chatContext', (e) => {
if ($(e.target).closest('.chatContext').length === 0) {
close();
}
});
}
function updateIgnoreText(id) {
if (settings.value(`${ignorePrefix}${id}`)) {
ignore.text(Translation.General('unignore'));
} else {
ignore.text(Translation.General('ignore'));
}
}
function close() {
container.hide();
container.off('.chatContext');
$('html').off('chatContext');
}
return {
open,
close,
};
})();
function processMessage(message, room) {
const id = message.id;
const user = message.user;
let info = $(`#${room} #message-${id} #info-${user.id}`);
if (!info.length) {
info = $(`#${room} #message-${id} #info-${id}`);
}
info.on('contextmenu.script.chatContext', {
name: fnUser.name(user),
staff: user.mainGroup.priority <= 6,
mod: user.mainGroup.priority <= 4,
id: user.id,
}, context.open);
}
eventManager.on('Chat:getHistory', (data) => {
JSON.parse(data.history).forEach((message) => {
processMessage(message, data.room);
});
});
eventManager.on('Chat:getMessage', function parse(data) {
if (this.canceled) return;
processMessage(JSON.parse(data.chatMessage), data.room);
});
});
eventManager.on('ChatDetected', () => {
toast = infoToast({
text: Translation.Toast('ignore.info'),
onClose: (reason) => {
toast = null; // Remove from memory
// return reason !== 'opened';
},
}, 'underscript.ignoreNotice', '1');
});