forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserInfoSidebar.user.js
304 lines (251 loc) · 10.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
// ==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 @samliew
// @version 2.2
//
// @include https://*stackoverflow.com/*
// @include https://*serverfault.com/*
// @include https://*superuser.com/*
// @include https://*askubuntu.com/*
// @include https://*mathoverflow.net/*
// @include https://*.stackexchange.com/*
//
// @include https://chat.stackexchange.com/*
// @include https://chat.meta.stackexchange.com/*
// @include https://chat.stackoverflow.com/*
//
// @exclude https://stackoverflow.com/c/*
// @exclude */admin/user-activity*
// @exclude */admin/dashboard*
//
// @require https://github.com/samliew/SO-mod-userscripts/raw/master/lib/common.js
//
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
// Moderator check
if(!isModerator()) return;
const isChat = location.hostname.includes('chat.');
function getChatParentUser() {
const parentuser = $('.user-stats a').last().attr('href');
return parentuser ? 'https:' + parentuser : '';
}
function getCurrentUserId() {
// Mod & CM messages
if(location.pathname.includes('/users/message/') || location.pathname.includes('/admin/cm-message/')) {
return $('.msg-moderator:first a[href^="/users/"], #addressing .user-details a').last().attr('href').match(/\d+/)[0];
}
// User & user admin pages
if(document.body.classList.contains('user-page') || (/[/-]users?[/-]/.test(location.href)) && document.body.classList.contains('mod-page')) {
return location.href.match(/\d+/)[0];
}
// Chat
if(isChat && location.pathname.includes('/users/')) {
const parentuser = getChatParentUser();
return parentuser ? parentuser.match(/\d+/)[0] : null;
}
// Question asker
const questionUser = $('#question .post-signature:last a[href*="/users/"]').first();
if(questionUser.length !== 0) {
return questionUser.attr('href').match(/\d+/)[0];
}
// Default
return null;
}
function doPageload() {
const uid = getCurrentUserId();
console.log(`Current User: ${uid}`);
if(!uid) return;
if(isChat) {
const userModPage = getChatParentUser().replace('/users/', '/users/account-info/').replace(/\D+$/, '');
const mainSiteHostname = userModPage.split('/users/')[0];
ajaxPromise(userModPage, 'document').then(function(data) {
const username = $('h1', data).first().get(0).childNodes[0].nodeValue.trim();
// Modify quicklinks and user details, then append to page
const $quicklinks = $('div.mod-links', data).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', data).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: red !important;' }).attr('title', `currently suspended (ends ${susDur})`);
}
// Append to page
$('body').append($quicklinks);
});
// Handle resize
$(window).on('load resize', function() {
$('body').toggleClass('usersidebar-open', $(document).width() >= 1400);
});
}
else {
// Get user's mod dashboard page
$.get('/users/account-info/' + uid, function(data) {
const username = $('h1', data).first().get(0).childNodes[0].nodeValue.trim();
// Modify quicklinks and user details, then append to page
const $quicklinks = $('div.mod-links', data).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', data).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(StackExchange.options.site.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) => StackExchange.options.site.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: red !important;' }).attr('title', `currently suspended (ends ${susDur})`);
}
// Append to page
$('body').append($quicklinks);
});
// Handle resize
$(window).on('load resize', function() {
$('body').toggleClass('usersidebar-open', $(document).width() >= 1660);
$('body').toggleClass('usersidebar-compact', $(window).height() <= 680);
});
}
}
function appendStyles() {
const styles = `
<style>
/* copied from main site as chat doesn't have this style */
.mod-links li {
margin-bottom: 5px;
}
.bounty-indicator-tab {
color: #FFF !important;
display: inline;
background-color: #0077dd;
padding: .2em .5em .25em;
margin-right: 5px;
font-size: 10px;
line-height: 1.3;
border-radius: 2px;
}
#usersidebar * {
box-sizing: border-box;
}
#usersidebar {
position: fixed;
z-index: 8950;
top: 44px;
right: 100%;
width: 190px;
max-height: calc(100vh - 50px);
padding: 10px 5px 0;
background: white;
opacity: 0.7;
border: 1px solid #ccc;
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: white;
border: 1px solid #ccc;
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-compact #usersidebar {
top: 0px;
max-height: 100vh;
}
.usersidebar-compact #usersidebar:after {
top: 49px;
}
#usersidebar h3 {
margin-bottom: 15px !important;
}
#usersidebar .details {
margin-bottom: 15px;
}
#usersidebar .details .info-header {
font-size: 0.95em;
font-style: italic;
color: #777;
}
#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;
}
.mod-quick-links .bounty-indicator-tab {
float: left;
margin-right: 4px !important;
}
</style>
`;
$('body').append(styles);
}
// On page load
appendStyles();
doPageload();
})();