-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStack Exchange, Flagging tweaks.user.js
455 lines (426 loc) · 18.9 KB
/
Stack Exchange, Flagging tweaks.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// ==UserScript==
// @name Stack Exchange, Flagging tweaks
// @description Adds flag-summary link to your profile pages that lack it. Color codes flagging history. Plus more.
// @match *://*.askubuntu.com/*
// @match *://*.mathoverflow.net/*
// @match *://*.serverfault.com/*
// @match *://*.stackapps.com/*
// @match *://*.stackexchange.com/*
// @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/*
// @exclude *://*.stackexchange.com/election/*
// @exclude *://*.stackoverflow.com/election/*
// @exclude *://stackoverflow.com/election/*
// @exclude *://api.stackexchange.com/*
// @exclude *://blog.stackexchange.com/*
// @exclude *://blog.stackoverflow.com/*
// @exclude *://chat.stackexchange.com/*
// @exclude *://data.stackexchange.com/*
// @exclude *://elections.stackexchange.com/*
// @exclude *://openid.stackexchange.com/*
// @exclude *://stackexchange.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @noframes
// @version 3.0
// @history 3.0 Stack Exchange significantly altered layouts. Flagging status up top (mods only). Restored/enhanced features for mods.
// @history 2.3 Adjusted flag icon for new page layout.
// @history 2.2 Prevent double icon that occurred in certain browsing sequences.
// @history 2.1 Always make sure you are testing the file you think you are testing!
// @history 2.0 Avoid mixed content warning on SSL pages. Add Tampermonkey metadata.
// @history 1.9 Flag summary post, edge case.
// @history 1.8 Updated to accommodate new topbar. Ref: meta.stackoverflow.com/q/343103/
// @history 1.7 For moderators, only summarize the mod's flags.
// @history 1.6 Block operation in iFrames.
// @history 1.5 Handle old-style flags.
// @history 1.0 Initial public release.
// @author Brock Adams
// @homepage http://stackapps.com/q/7057/7653
// ==/UserScript==
/* global $ */
/* eslint-disable no-multi-spaces, curly */
var userId;
if (objHas (unsafeWindow, "StackExchange.options.user.userId") ) {
userId = unsafeWindow.StackExchange.options.user.userId;
}
else {
console.log ("===> ", GM_info.script.name + ": no user detected.");
// Note: on double-runs caused by rapid redirects, the first pass will report no user. This is okay and only the last run counts.
}
/* Other potential icons:
//-- Mops, brooms:
http://i.stack.imgur.com/05rE1.png -- Transp white outline, transparent.
http://i.stack.imgur.com/Y9Ffx.png -- White outline, transparent.
http://i.stack.imgur.com/m5n2A.png -- White outline, black BG.
//-- Skulls:
http://i.stack.imgur.com/yfleb.png -- Darker blue
http://i.stack.imgur.com/yTdZC.png -- Green
http://i.stack.imgur.com/wU37v.png -- Overly light green?
http://i.stack.imgur.com/0CLsA.png -- Green, with light fill
http://i.stack.imgur.com/4ti36.png -- Blue, with light green fill
http://i.stack.imgur.com/78mbl.png -- Blue, with transparent white fill
*/
var flagSumIconSrc = "//i.stack.imgur.com/05rE1.png";
/*--- On user profile pages, add a "Flag Summary" tab if the user can see flag details..
1) Either the user is logged in and looking at his own profile...
2) Or, the user is a moderator.
*/
if ( /\/users\/\d+/.test (location.pathname) ) {
let editTabNewStyle = $("#mainbar-full #tabs > a[href^='/users/edit/'], #mainbar-full #tabs > a[href^='/users/preferences/']").first ();
if (editTabNewStyle.length) {
//--- We're on an eligible profile page.
let pgUsrMtch = location.pathname.match (/users\/(\d+)/);
if (pgUsrMtch && pgUsrMtch.length > 1) {
let viewedUsrId = pgUsrMtch[1];
editTabNewStyle.after ('<a href="/users/flag-summary/' + viewedUsrId + '">Flag Summary</a>');
}
}
}
/*--- On flagging summary pages...
*/
if ( /\/users\/flag-summary\/\d+/.test (location.pathname) ) {
let changeDsp = null;
let bViewedUsrIsLoggedUsr = false;
let filterTableKeyNode = $("#flag-summary-filter");
if (filterTableKeyNode.length === 0) console.warn ("Flag summary script can't find `flag-summary-filter` node.");
//--- If Flagging status is present, move it up top, since it is much more important.
let flagStatNode1 = filterTableKeyNode.nextAll (".s-sidebarwidget--header:contains(tatus)");
if (flagStatNode1.length) {
let flagStatNode2 = flagStatNode1.next ();
flagStatNode1.insertBefore (filterTableKeyNode);
flagStatNode2.insertBefore (filterTableKeyNode);
}
//--- Color-code each post's flags listing...
$(".flagged-post").each ( function () {
//--- Posts can have multiple flags, so may have to stripe each flag separately
var jThis = $(this);
var postsFlags = jThis.find (".mod-flag");
if (postsFlags.length === 0) {
//--- Oops. This should not happen!
jThis.css ('background', 'red');
reportImportantError ("Page structure change or error in flag report markup");
}
else if (postsFlags.length === 1) {
//--- Simpler case
styleNodeBasedOnFlagContents (jThis);
}
else {
//--- More than 1 flag per post.
var indFlagLines = jThis.find (".mod-flag");
indFlagLines.each ( function () {
var jThisL = $(this);
styleNodeBasedOnFlagContents (jThisL);
} );
//--- Now color the overall entry based on subentry stats:
if (jThis.find (".gmFlagPending").length) {
jThis.addClass ('gmFlagPending');
}
else if (jThis.find (".gmFlagDeclined").length) {
jThis.addClass ('gmFlagDeclined');
}
else if (jThis.find (".gmFlagDisputed").length) {
jThis.addClass ('gmFlagDisputed');
}
else if (jThis.find (".gmFlagHelpful").length) {
jThis.addClass ('gmFlagHelpful');
}
else if (jThis.find (".gmFlagRetracted").length) {
jThis.addClass ('gmFlagRetracted');
}
else {
jThis.css ('background', "red");
reportImportantError ("Unresolved Overall flag entry!", false, jThis);
}
}
} );
//--- Create summary table:
var statsArray = [
0, //-- Total for percent calcs
0, //-- status=1 waiting for review <-- Stored 0
0, //-- status=2 helpful <-- Stored 1
0, //-- status=3 declined <-- Stored 2
0, //-- status=4 disputed <-- Stored 3
0, //-- status=5 expired, AKA "Aged Away" <-- Stored 4
0, //-- Grand Total <-- Stored 5
0, //-- New Retracted status <-- Stored 6
// percentHelp <-- Stored 7
];
/*--- Loop thru History table entries and tot up stats:
<li> <a href="?group=1&status=2" class="grid bb bc-black-1">
<div class="grid--cell">helpful</div>
<div class="grid--cell ta-right fl1">17</div>
</a> </li>
*/
filterTableKeyNode.nextAll ("ul.s-sidebarwidget__items").find ("li > a[href*='status']").each ( function () {
var jThis = $(this);
var fType = parseInt (jThis.attr ("href").replace (/^.+?\bstatus=(\d+).*$/, "$1"), 10);
var fCount = parseInt (jThis.children (".ta-right").text ().replace (/\D/g, ""), 10);
statsArray[6] += fCount; //-- Increment grand total, always.
switch (fType) {
case 1: //-- Pending
case 4: //-- Disputed
case 5: //-- Expired
statsArray[fType] += fCount;
/*-- "Pending" doesn't count for percent calcs.
Likewise, from http://meta.stackexchange.com/a/141400/148310 :
"The flag weight is decreased by declined flags, but not by disputed ones. "
We were *encouraged*, at one point to dispute flags, too.
*/
break;
case 6: //-- Retracted
statsArray[7] += fCount; //-- Because added late by SE, index is off if we want to preserve old stats.
break;
case 2: //-- Helpful
case 3: //-- Declined
statsArray[fType] += fCount;
statsArray[0] += fCount; //-- Also used for percent calcs
break;
default:
reportImportantError ("Unexpected case while parsing flag history table: ", false, fType);
break;
}
} );
let percentHelp = Math.round (100.0 * statsArray[2] / (statsArray[0] || 1) );
//--- Only fetch and updated stored stats if the flag summary page matches the logged-in user.
let pgUsrMtch = location.pathname.match (/flag-summary\/(\d+)/);
if (pgUsrMtch && pgUsrMtch[1] == userId) {
bViewedUsrIsLoggedUsr = true;
let siteNameKey = location.hostname;
/*--- IMPORTANT!
We DON't store the first column (Total for percent calcs) using GM_setValue
but we do add and store percentHelp to the array.
*/
let oldStats = JSON.parse (GM_getValue (siteNameKey, "[0,0,0,0, 0,0,0,0]") );
if (oldStats.length !== 8) {
//-- Preserve old stats AMAP, while adding new columns at end...
console.log ("GM: Stat array changed! Compensating...");
oldStats = oldStats.slice (0,7).concat (0);
}
let newStats = statsArray.slice (1,8).concat (percentHelp);
GM_setValue (siteNameKey, JSON.stringify (newStats) );
changeDsp = $.map (newStats, function (newValue, J) {
var diff = newValue - oldStats[J];
var dspStr = diff < 0 ? diff : (diff > 0 ? "+" + diff : " ");
return dspStr;
} );
}
filterTableKeyNode.parent ().before ( `
<table id="gmFlagStatTbl">
<tr><th title="Pending">P</th> <th title="Helpful">H</th> <th title="Declined">Dc</th>
<th title="Disputed">Ds</th> <th title="Expired">Ex</th> <th title="Retracted">R</th>
<th title="Total">T</th> <th title="Percent Helpful">%</th>
</tr><tr>
<td title="Click to filter.">` + statsArray[1] + `</td>
<td title="Click to filter.">` + statsArray[2] + `</td>
<td title="Click to filter.">` + statsArray[3] + `</td>
<td title="Click to filter.">` + statsArray[4] + `</td>
<td title="Click to filter.">` + statsArray[5] + `</td>
<td title="Click to filter.">` + statsArray[7] + `</td> <!-- Newly added status type -->
<td title="Click to RESET filter.">` + statsArray[6] + `</td>
<td title="Click to RESET filter.">` + percentHelp + `</td>
</tr>
</table>
` );
if (bViewedUsrIsLoggedUsr) {
$("#gmFlagStatTbl").append ( `
<tr>
<td title="Change since last page view">` + changeDsp[0] + `</td>
<td title="Change since last page view">` + changeDsp[1] + `</td>
<td title="Change since last page view">` + changeDsp[2] + `</td>
<td title="Change since last page view">` + changeDsp[3] + `</td>
<td title="Change since last page view">` + changeDsp[4] + `</td>
<td title="Change since last page view">` + changeDsp[6] + `</td> <!-- Newly added status type -->
<td title="Change since last page view">` + changeDsp[5] + `</td>
<td title="Change since last page view">` + changeDsp[7] + `</td>
</tr>
` );
}
/*--- Linkify the table cells. Can get more useful results than page's filters.
URL's like: http://superuser.com/users/flag-summary/41023?status=3
*/
$("#gmFlagStatTbl").on ("click", "td, th", function (zEvent) {
var filterURL = location.pathname;
var columnIdx = zEvent.target.cellIndex;
switch (columnIdx) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
filterURL += '?status=' + (columnIdx + 1);
break;
case 6:
case 7:
//-- No change needed to filterURL, cause going to show all results.
break;
default:
reportImportantError ("Unexpected case in summary table, click handler: ", false, columnIdx);
break;
}
location.assign (filterURL);
} );
}
else {
//--- Not on flagging summary page.
//--- Add quicklink for flag summary in topbar
if (userId) {
//-- New topbar style as of about April 19, 2018
if (document.getElementById("gmFlagSumIco") == null) {
$(".my-profile").parent ().before ( `
<li class="-item">
<a class="my-profile" href="/users/flag-summary/${userId}" title="Flag Summary page">
<div class="grayBG gravatar-wrapper-24">
<img id="gmFlagSumIco" class="avatar-me js-avatar-me" src="${flagSumIconSrc}">
</div>
</a>
</li>
` );
}
}
//--- Check if we are too late to flag an answer:
var qstMtch = location.pathname.match (/\/questions\/(\d+)\/.+?\/(\d+)\/?$/);
if (qstMtch && qstMtch.length > 2) {
var ansId = qstMtch[2];
var ansPost = $("#answer-" + ansId);
if (ansPost.length === 0) {
var alrtPopup = $('<div id="gmAnswerGoneAlrt">The linked answer is gone from this page.</div>').appendTo ("body");
alrtPopup.append ('<div class="small">(Click to close this message)</div>');
alrtPopup.click ( function () { $(this).hide (); } );
}
}
}
function styleNodeBasedOnFlagContents (jNode) {
if (jNode.has (".Declined").length) {
jNode.addClass ('gmFlagDeclined');
}
else if (jNode.has (".Disputed").length) {
jNode.addClass ('gmFlagDisputed');
}
else if (jNode.has (".ScheduledTaskInvalidated").length) {
jNode.addClass ('gmFlagDisputed');
}
else if (jNode.has (".Helpful").length) {
jNode.addClass ('gmFlagHelpful');
}
else if (jNode.has (".SelfClear").length) {
jNode.addClass ('gmFlagRetracted');
}
//--- Unresolved flags = poor SE design too
else if (jNode.has (".supernovabg").length) {
jNode.addClass ('gmFlagPending');
}
else {
/*--- Last ditch check for old-style flags. These were not tracked the modern way.
The change happened approximately April 28th, 2011.?
*/
var chngDate = new Date ("2011-04-28");
var dateNd = jNode.find (".relativetime");
var flagDate = new Date (dateNd.attr ("title") );
if (flagDate < chngDate) {
jNode.addClass ('gmObsolete');
}
else {
jNode.css ('background', "red");
reportImportantError ("Unresolved flag entry!", false, jNode);
}
}
}
function reportImportantError (errMess, bNoAlert) {
console.error ("Flagging script: " + errMess);
if (arguments.length > 2) {
console.error.apply (console, Array.prototype.slice.call (arguments, 2) );
}
if ( ! bNoAlert) {
//-- On SE pages, use SE's error popup:
var targNode = $("#hmenus");
var seUtils = unsafeWindow.StackExchange;
if (targNode.length && objHas (seUtils, "helpers.showErrorPopup") ) {
//-- Can't pass targNode because of sandbox...
seUtils.helpers.showErrorPopup ("#hmenus", "Page structure change or script error. See Console.");
}
else {
alert ("Page structure change or script error. See Console.");
}
}
}
function objHas (obj, key) {
//-- Needed because .hasOwnProperty() does not work for nested properties. :(
return key.split (".").every (_rcrsvlyChkPropname);
//-- Declared function for better performance...
function _rcrsvlyChkPropname (propName) {
if ( obj === null || typeof obj !== "object" || ! (propName in obj) ) {
return false;
}
obj = obj[propName];
return true;
}
}
GM_addStyle ( `
#gmFlagStatTbl {
margin: -1.4ex 0ex 2ex 0ex;
width: 100%;
}
#gmFlagStatTbl td, #gmFlagStatTbl th{
padding: 0.6ex 0.2ex 0.6ex 0.6ex;
border: 1px solid lightgray;
text-align: left;
width: 5em;
cursor: pointer;
background-color: #f0f8ff; // very light blue-ish
}
.gmFlagPending, #gmFlagStatTbl td:nth-child(1), #gmFlagStatTbl th:nth-child(1) {
background-color: lightyellow;
}
.gmFlagHelpful, #gmFlagStatTbl td:nth-child(2), #gmFlagStatTbl th:nth-child(2) {
background-color: #eeffee; // very light green
}
.gmFlagDeclined, #gmFlagStatTbl td:nth-child(3), #gmFlagStatTbl th:nth-child(3) {
background-color: pink;
}
.gmFlagDisputed,
#gmFlagStatTbl td:nth-child(4), #gmFlagStatTbl th:nth-child(4),
#gmFlagStatTbl td:nth-child(5), #gmFlagStatTbl th:nth-child(5) {
background-color: #fbc97f; // light orange
}
.gmFlagRetracted, #gmFlagStatTbl td:nth-child(6), #gmFlagStatTbl th:nth-child(6) {
background-color: #f2e6ff; // lavender-y
}
.gmObsolete {
background-color: lightgray;
}
#gmFlagStatTbl tr:nth-child(3) > td {
background: none;
border: none;
padding: 0 1px 0 4px;
}
#gmFlagSumIco {
width: 20px;
height: 20px;
}
#gmAnswerGoneAlrt {
position: fixed;
top: 2rem;
width: 60vw;
background: pink;
font-size: 2rem;
padding: 2rem;
border: 1px solid blue;
border-radius: 0.5ex;
left: calc(50% - 36vw);
cursor: pointer;
}
#gmAnswerGoneAlrt > .small {
font-size: 1rem;
margin: 1.5rem 0 -1rem 0;
color: gray;
}
.grayBG {
background: gray;
}
` );
/****************************************************************************
***** End of File *****
****************************************************************************/