-
Notifications
You must be signed in to change notification settings - Fork 33
/
UserInfoSidebar.user.js
410 lines (339 loc) · 13.4 KB
/
UserInfoSidebar.user.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// ==UserScript==
// @name User Info Sidebar
// @description Adds user moderation links sidebar with quicklinks & user details (from Mod Dashboard) to user-specific pages
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 4.2.13
//
// @match https://*.stackoverflow.com/*
// @match https://*.serverfault.com/*
// @match https://*.superuser.com/*
// @match https://*.askubuntu.com/*
// @match https://*.mathoverflow.net/*
// @match https://*.stackapps.com/*
// @match https://*.stackexchange.com/*
// @match https://stackoverflowteams.com/*
//
// @match https://chat.stackexchange.com/*
// @match https://chat.meta.stackexchange.com/*
// @match https://chat.stackoverflow.com/*
//
// @exclude */admin/user-activity*
// @exclude */admin/dashboard*
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *blog.*
// @exclude */tour
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
//
// @grant GM_xmlhttpRequest
// ==/UserScript==
/* globals StackExchange, isMetaSite, parentUrl */
/// <reference types="./globals" />
'use strict';
// This is a moderator-only userscript
if (!isModerator()) return;
let uid;
function getCurrentUserId() {
// Chat
const chatUser = isChat && location.pathname.includes('/users/') &&
document.querySelector('.user-stats a[href*="/users/"]')?.href.match(/\d+/)?.shift();
// Question Author
const isQuestionPage = location.pathname.startsWith('/questions/');
const questionUser = isQuestionPage && [...document.querySelectorAll('#question .post-signature')].pop()?.querySelector('a[href*="/users/"]')?.href.match(/\d+/)?.shift();
// Default
return Number(chatUser || questionUser || currentUserId) || null;
}
function doChatSidebar() {
const parentUserHref = document.querySelector('.user-stats a[href*="/users/"]')?.href;
if (!parentUserHref) return;
const userDashboardPage = parentUserHref.replace('/users/', '/users/account-info/').replace(/\D+$/, '');
const mainSiteHostname = userDashboardPage.split('/users/')[0];
ajaxPromise(userDashboardPage).then(function (data) {
const modContent = $('#mod-content', data);
// Get username
const username = $('h1', modContent).first().get(0).childNodes[0].nodeValue.trim();
// Modify quicklinks and user details, then append to page
const quickLinks = $('div.mod-links', modContent).attr('id', 'usersidebar');
const modActions = quickLinks.find('.mod-actions');
// Move contact links
modActions.find('li').slice(-4, -2).appendTo(quickLinks.find('ul:first'));
// Remove other actions as they need additional work to get popup working
modActions.last().remove();
// Headers
const infoHeader = quickLinks.find('h3').last().text(username).prependTo(quickLinks);
// Insert user details
const info = $('.mod-section .details', modContent).insertAfter(infoHeader);
info.children('.row').each(function () {
$(this).children().first().unwrap();
});
// Transform user details to list format
info.children('.col-2').removeClass('col-2').addClass('info-header');
info.children('.col-4').removeClass('col-4').addClass('info-value');
// Change xref link to month to be more useful (default was week)
quickLinks.find('a[href*="xref-user-ips"]').attr('href', (i, v) => v += '?daysback=30&threshold=2');
// Prepend Mod dashboard link
quickLinks.find('ul').prepend(`<li><a href="/users/account-info/${uid}">mod dashboard</a></li>`);
// Since we are on chat, transform links to main links
$('a[href^="/"]', info).attr('href', (i, v) => mainSiteHostname + v);
$('.mod-quick-links a', quickLinks).attr('href', (i, v) => mainSiteHostname + v);
// Check if user is currently suspended, highlight username
const susMsg = $('.system-alert', data).first().text();
if (susMsg.indexOf('suspended') >= 0) {
const susDur = susMsg.split('ends')[1].replace(/(^\s|(\s|\.)+$)/g, '');
quickLinks.find('h3').first().attr({ style: 'color: var(--red-500) !important;' }).attr('title', `currently suspended (ends ${susDur})`);
}
// Add links to all three chat domains
const chatlinkSO = info.find('a[href^="https://chat."]').text('SO').attr('href', function (i, href) {
return href.replace('//accounts', '/accounts').replace(/(?:meta\.)?stackexchange\.com/, 'stackoverflow.com');
}).addClass('d-inline-block mr12 fs-body2');
const chatlinkSE = chatlinkSO.clone(true).attr('href', function (i, href) {
return href.replace('stackoverflow.com', 'stackexchange.com');
}).text('SE').insertAfter(chatlinkSO);
const chatlinkMSE = chatlinkSO.clone(true).attr('href', function (i, href) {
return href.replace('stackoverflow.com', 'meta.stackexchange.com');
}).text('MSE').insertAfter(chatlinkSE);
// Links in sidebar open in new tabs
quickLinks.find('a').attr('target', '_blank');
// Add bgcolor to num of annotations and flags for user
$('.bounty-indicator-tab', quickLinks).slice(0, 2).each(function (i, el) {
const linkText = $(this).next('a').text();
el.classList.add(linkText.includes('annotations') ? 'supernovabg' : 'hotbg');
});
// Append to page
$('body').append(quickLinks);
});
// Show sidebar on desktop
const isDesktop = () => $(document).width() >= 1400;
const updateOpenSidebar = () => {
$('body').toggleClass('usersidebar-open', isDesktop());
};
// On page load and resize
updateOpenSidebar();
$(document).on('ready', updateOpenSidebar);
$(window).on('load resize', updateOpenSidebar);
}
function doMainSidebar() {
// If on user dashboard page
if (location.pathname.includes('/users/account-info/')) {
// Add bgcolor to num of annotations and flags for user
$('.mod-links .bounty-indicator-tab').slice(0, 2).each(function (i, el) {
const linkText = $(this).next('a').text();
el.classList.add(linkText.includes('annotations') ? 'supernovabg' : 'hotbg');
});
// If on meta,
if (isMetaSite) {
// Enable contact user link (auto redirects to main)
$('.mod-links span.disabled').filter((i, v) => v.innerText.includes('contact user')).replaceWith(`<a title="use to contact this user and optionally suspend them" href="/users/message/create/${uid}">contact user</a>`);
}
// Don't do sidebar
//return;
}
// Get user's mod dashboard page
$.get('/users/account-info/' + uid, function (data) {
// If deletion record not found, do nothing
if (data.includes('Could not find a user or deletion record')) return;
const modContent = $('#mod-content', data);
// Get username
const username = $('h1', modContent).first().get(0)?.childNodes[0].nodeValue.trim();
if(!username) return; // deleted user
// Modify quicklinks and user details, then append to page
const quickLinks = $('div.mod-links', modContent).attr('id', 'usersidebar');
const modActions = quickLinks.find('.mod-actions');
// Move contact links
modActions.find('li').slice(-4, -2).appendTo(quickLinks.find('ul:first'));
// Remove other actions as they need additional work to get popup working
modActions.last().remove();
// Headers
const infoHeader = quickLinks.find('h3').last().text(username).prependTo(quickLinks);
// Insert user details
const info = $('.mod-section .details', modContent).insertAfter(infoHeader);
info.children('.row').each(function () {
$(this).children().first().unwrap();
});
// Transform user details to list format
info.children('.col-2').removeClass('col-2').addClass('info-header');
info.children('.col-4').removeClass('col-4').addClass('info-value');
// Change xref link to month to be more useful (default was week)
quickLinks.find('a[href*="xref-user-ips"]').attr('href', (i, v) => v += '?daysback=30&threshold=2');
// Prepend Mod dashboard link
quickLinks.find('ul').prepend(`<li><a href="/users/account-info/${uid}">mod dashboard</a></li>`);
// If on meta,
if (isMetaSite) {
// Enable contact user link
$('.mod-quick-links span.disabled', quickLinks).replaceWith(`<a title="use to contact this user and optionally suspend them" href="/users/message/create/${uid}">contact user</a>`);
// change links to main
$('.mod-quick-links a', quickLinks).attr('href', (i, v) => parentUrl + v);
}
// Check if user is currently suspended, highlight username
const susMsg = $('.system-alert', data).first().text();
if (susMsg.indexOf('suspended') >= 0) {
const susDur = susMsg.split('ends')[1].replace(/(^\s|(\s|\.)+$)/g, '');
quickLinks.find('h3').first().attr({ style: 'color: var(--red-500) !important;' }).attr('title', `currently suspended (ends ${susDur})`);
}
// Add links to all three chat domains
const chatlinkSO = info.find('a[href^="https://chat."]').text('SO').attr('href', function (i, href) {
return href.replace('//accounts', '/accounts').replace(/(?:meta\.)?stackexchange\.com/, 'stackoverflow.com');
}).addClass('d-inline-block mr12 fs-body2');
const chatlinkSE = chatlinkSO.clone(true).attr('href', function (i, href) {
return href.replace('stackoverflow.com', 'stackexchange.com');
}).text('SE').insertAfter(chatlinkSO);
const chatlinkMSE = chatlinkSO.clone(true).attr('href', function (i, href) {
return href.replace('stackoverflow.com', 'meta.stackexchange.com');
}).text('MSE').insertAfter(chatlinkSE);
// Links in sidebar open in new tabs
quickLinks.find('a').attr('target', '_blank');
// Add bgcolor to num of annotations and flags for user
$('.bounty-indicator-tab', quickLinks).slice(0, 2).each(function (i, el) {
const linkText = $(this).next('a').text();
el.classList.add(linkText.includes('annotations') ? 'supernovabg' : 'hotbg');
});
// Append to page
$('body').append(quickLinks);
});
// Handle resize
$(window).on('load resize', function () {
$('body').toggleClass('usersidebar-open', $(document).width() >= 1720);
});
}
// Append styles
addStylesheet(`
.s-table th, .s-table td {
padding: 3px;
}
.js-profile-mod-info table td.mod-label {
font-weight: bold;
}
/* copied from main site as chat doesn't have this style */
.mod-links li {
margin-bottom: 5px;
}
.bounty-indicator-tab {
color: var(--white) !important;
display: inline;
background-color: var(--blue-600);
padding: 0.25em 0.5em 0.1em;
margin-right: 5px;
font-size: var(--fs-fine);
line-height: 1.3;
border-radius: 2px;
}
.bounty-indicator-tab.supernovabg {
background: var(--orange-400) !important;
}
.bounty-indicator-tab.hotbg {
background-color: var(--orange-600) !important;
}
#usersidebar * {
box-sizing: border-box;
}
#usersidebar {
position: fixed;
z-index: 8950;
top: 44px;
right: 100%;
width: 230px;
max-height: calc(100vh - 50px);
padding: 10px 12px 0;
background: var(--white);
opacity: 0.7;
border: 1px solid var(--black-150);
box-shadow: 2px 2px 14px -3px rgba(0,0,0,0.25);
}
#usersidebar:after {
content: 'user';
position: absolute;
left: 100%;
top: 5px;
width: 40px;
height: 30px;
padding: 5px 8px;
background: var(--white);
border: 1px solid var(--black-150);
border-left: none;
box-shadow: 3px 2px 10px -2px rgba(0,0,0,0.25);
box-sizing: border-box;
}
.usersidebar-open #usersidebar,
#usersidebar:hover {
left: -1px;
right: initial;
opacity: 1;
}
.usersidebar-open #usersidebar {
top: 50px;
box-shadow: none;
}
.usersidebar-open #usersidebar:after {
display: none;
}
#usersidebar .profile-section-title {
margin-bottom: 15px !important;
padding-left: 0px !important;
}
#usersidebar .details {
margin-bottom: 15px;
}
#usersidebar .details .info-header {
font-size: 0.95em;
font-style: italic;
color: var(--black-500);
}
#usersidebar .details .info-value {
margin-bottom: 10px;
}
#usersidebar .details > div:nth-child(-n + 2),
#usersidebar .details > div:nth-child(n+7):nth-child(-n+8),
#usersidebar .details > div:nth-child(n+13):nth-child(-n+14),
#usersidebar .details > div:nth-child(n+19) {
display: none;
}
#usersidebar .details a[href^="https://stackexchange.com/users/"] {
font-size: var(--fs-body2);
}
.mod-quick-links .bounty-indicator-tab {
float: left;
margin-right: 4px !important;
}
/* Fullscreen snippets always on top */
.snippet.expanded-snippet {
z-index: 9999999 !important;
}
@media screen and (max-height: 740px) {
#usersidebar {
top: 0px !important;
max-height: 100vh;
}
#usersidebar:after {
top: 49px;
}
#usersidebar .details {
line-height: 1.2;
}
#usersidebar ul li {
margin-bottom: 2px;
}
}
`); // end stylesheet
// On script run
(function init() {
// If on account info dashboard page, show username in page title
if (location.pathname.includes('/users/account-info/')) {
const username = $('#mod-content h1').text().trim().replace(/\s+\(.+$/g, '').trim();
document.title = username + ' - Account Information';
}
setTimeout(() => {
uid = getCurrentUserId();
if (!uid) {
console.log('UIS: No current user ID');
return;
}
// If on chat page, show chat sidebar
isChat ? doChatSidebar() : doMainSidebar();
}, 100);
})();