-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext.js
More file actions
209 lines (200 loc) · 6.52 KB
/
context.js
File metadata and controls
209 lines (200 loc) · 6.52 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
settings.register({
name: 'Disable Chat Context (right click)',
key: 'underscript.disable.chatContext',
page: 'Chat',
});
eventManager.on('ChatDetected' , () => {
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: 0px 5px; height: auto; }',
'.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: #003366; color: #F2F2F2; }',
'.chatContext > :last-child { border-radius: 0 0 10px 10px; }'
);
let toast;
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>');
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',
};
fn.each(times, (item, key) => {
muteTime.append($(`<option value="${key}"${key === '3600' ? ' selected':''}>${item}</option>`));
});
mute.append(' ', muteTime);
function open(event) {
if (event.ctrlKey || settings.value('underscript.disable.chatContext')) 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 selfMod = selfId !== id && selfMainGroup.priority <= 4;
if (selfMod || 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 = 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]) {
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 += `@${fn.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(`${ignorePrefix}${id}`)) {
localStorage.setItem(key, name);
fn.toast({
text: `You've ignored ${name}`,
css: {
'background-color': 'rgba(208, 0, 0, 0.6)',
},
buttons: [{
css: {
border: '',
height: '',
background: '',
'font-size': '',
'margin': '',
'border-radius': '',
},
text: 'Undo',
className: 'dismiss',
onclick: () => {
settings.remove(key);
updateIgnoreText(id);
},
},],
className: 'dismissable',
});
fn.ignoreUser(name, key);
} else {
settings.remove(key);
}
updateIgnoreText(id);
} else if (e.target === mute[0]) {
if (muteDisabled) return;
timeout(`${id}`, muteTime.val());
} else if (e.target === message[0]) {
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', (event) => {
if ($(event.target).closest('.chatContext').length === 0) {
close();
}
});
}
function updateIgnoreText(id) {
if (settings.value(`${ignorePrefix}${id}`)) {
ignore.html('Unignore');
} else {
ignore.html('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;
const name = user.username;
let info = $(`#${room} #message-${id} #info-${user.id}`);
if (!info.length) {
info = $(`#${room} #message-${id} #info-${id}`);
}
info.on('contextmenu.script.chatContext', {
name,
staff: user.mainGroup.priority <= 6,
mod: user.mainGroup.priority <= 4,
id: user.id,
}, context.open);
}
eventManager.on('Chat:getHistory', function (data) {
JSON.parse(data.history).forEach((message) => {
processMessage(message, data.room);
});
});
eventManager.on('Chat:getMessage', function (data) {
if (this.canceled) return;
processMessage(JSON.parse(data.chatMessage), data.room);
});
toast = fn.infoToast({
text: 'You can right click users in chat to display user options!',
onClose: (reason) => {
toast = null; // Remove from memory
//return reason !== 'opened';
}
}, 'underscript.ignoreNotice', '1');
});