-
Notifications
You must be signed in to change notification settings - Fork 34
/
RejectedSuggestedEdits.user.js
343 lines (292 loc) · 11 KB
/
RejectedSuggestedEdits.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
// ==UserScript==
// @name Rejected Suggested Edits
// @description New page to review rejected suggested edits
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 3.0.13
//
// @match https://*.stackoverflow.com/review/suggested-edits*
// @match https://*.serverfault.com/review/suggested-edits*
// @match https://*.superuser.com/review/suggested-edits*
// @match https://*.askubuntu.com/review/suggested-edits*
// @match https://*.mathoverflow.net/review/suggested-edits*
// @match https://*.stackapps.com/review/suggested-edits*
// @match https://*.stackexchange.com/review/suggested-edits*
// @match https://stackoverflowteams.com/c/*/review/suggested-edits*
//
// @match https://*.stackoverflow.com/admin/links
// @match https://*.serverfault.com/admin/links
// @match https://*.superuser.com/admin/links
// @match https://*.askubuntu.com/admin/links
// @match https://*.mathoverflow.net/admin/links
// @match https://*.stackapps.com/admin/links
// @match https://*.stackexchange.com/admin/links
// @match https://stackoverflowteams.com/c/*/admin/links
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *chat.*
// @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
// ==/UserScript==
/* globals StackExchange */
/// <reference types="./globals" />
'use strict';
const apikey = 'FHd6ejY4s4KDhL9VCWhkwQ((';
const resultsDiv = $(`<div id="reviews"></div>`);
const pagerDiv = $(`<div class="s-pagination pager fl"></div>`);
// Helper Functions
function getRedirectUrl(url, method = "GET") {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (e) {
if (xhr.status == 200 && xhr.readyState == 4) {
if (url != xhr.responseURL) {
resolve(xhr.responseURL);
} else {
reject();
}
}
}
xhr.open(method, url, true);
xhr.send();
});
}
function buildPagination(max, page = 1, baseUrl = "", range = 5) {
pagerDiv.empty();
let str = '';
let from = page - range;
let to = page + range;
if (from < 1) from = 1;
if (to > max) to = max;
if (page - 1 >= 1) {
str += `<a href="${baseUrl}?page=${page - 1}" class="s-pagination--item" rel="prev">Prev</a> `;
}
if (from > 1) {
str += `<a href="${baseUrl}?page=1" class="s-pagination--item">1</a> `;
if (from > 2) {
str += `<div class="s-pagination--item s-pagination--item__clear">…</span> `;
}
}
for (let i = from; i <= to; i++) {
i == page ?
str += `<div class="s-pagination--item is-selected">${i}</div> ` :
str += `<a href="${baseUrl}?page=${i}" class="s-pagination--item">${i}</a> `;
}
if (to < max) {
if (to < max - 1) {
str += `<div class="s-pagination--item s-pagination--item__clear">…</div> `;
}
str += `<a href="${baseUrl}?page=${max}" class="s-pagination--item">${max}</a> `;
}
if (page + 1 <= max) {
str += `<a href="${baseUrl}?page=${page + 1}" class="s-pagination--item" rel="next">Next</a> `;
}
pagerDiv.html(str);
}
function getRejected(page = 1) {
// Check for backoff
if (hasBackoff()) { return; }
resultsDiv.empty();
StackExchange.helpers.addSpinner('#reviews');
$.get(`${seApiUrl}/suggested-edits?page=${page}&pagesize=100&order=desc&sort=rejection&filter=!*KkBP6Je7loS9)xf&site=${location.hostname}&key=${apikey}`, function (data) {
StackExchange.helpers.removeSpinner();
addBackoff(data.backoff);
const items = data.items;
let html = '';
items.forEach(function (v) {
const posttype = v.post_type.charAt(0);
const creationDate = new Date(v.creation_date * 1000);
const rejectionDate = new Date(v.rejection_date * 1000);
const proposingUser = !v.proposing_user ? '<span class="userlink">anonymous</span>' :
`<a class="userlink" href="${v.proposing_user.link}" title="rep: ${v.proposing_user.reputation}, type: ${v.proposing_user.user_type}">${v.proposing_user.display_name}</a>`;
html += `
<div class="review review-bar-container">
<a href="/suggested-edits/${v.suggested_edit_id}" class="toggle" title="toggle review summary"></a>
<span class="userspan">${proposingUser}</span>
<a href="/suggested-edits/${v.suggested_edit_id}">suggested edit</a>
on <a href="/${posttype}/${v.post_id}" class="answer-hyperlink">${posttype}${v.post_id}</a>
was rejected <span title="${dateToIsoString(rejectionDate)}" class="relativetime">${dateToRelativeTime(rejectionDate)}</span>
</div>`;
});
resultsDiv.append(html);
StackExchange.realtime.updateRelativeDates();
buildPagination(Math.ceil(data.total / data.page_size), data.page);
// Load anonymous details if <= 10
const anonusers = $('span.userlink');
if (anonusers.length <= 10) anonusers.parent().siblings('.toggle').trigger('preload');
});
}
function initRejectsPage() {
document.title = `Rejected Reviews - Suggested Edits`;
// Insert nav and results container
const content = $('#content')
.html('<div id="mainbar" role="main" class="grid"></div>')
.append(`
<div class="subheader tools-rev">
<h1><a href="/review">Review</a><span class="lsep">|</span><span class="review-title">Rejected Suggested Edits</span></h1>
<div id="tabs">
<a href="/review/suggested-edits/stats">stats</a>
<a href="/review/suggested-edits/history">history</a>
<a href="/review/suggested-edits/history/rejected" class="youarehere">rejects</a>
<a href="/review/suggested-edits">review</a>
</div>
</div>`).append(resultsDiv).append(pagerDiv);
// Get list from API
getRejected(1);
// Update timings every 30 secs, doesn't work with toggle time format
//setInterval(function() {
// StackExchange.realtime.updateRelativeDates();
//}, 30000);
// Handle same-page ajax pagination
pagerDiv.on('click', 'a', function (evt) {
const pagenum = Number($(this).attr('href').match(/page=\d+/)[0].split('=')[1]);
getRejected(pagenum);
return false;
});
// Toggle time format
$(`<a class="toggle-date-format">toggle format</a>`).insertBefore(resultsDiv).on('click', function () {
$('#reviews .review > .relativetime').each(function (i, el) {
// Switch title and text
const tmp = el.innerText;
el.innerText = el.title;
el.title = tmp;
});
StackExchange.realtime.updateRelativeDates();
});
// Toggle open review
resultsDiv.on('click preload', '.toggle', function (evt) {
const preload = evt.type == 'preload';
const rev = $(this).parent();
const url = $(this).attr('href');
if (!preload) {
rev.toggleClass('open');
}
if (rev.children('.review-info').length == 0) {
const infoDiv = $(`<div class="review-info review-bar"></div>`).appendTo(rev);
if (preload) {
infoDiv.hide();
}
StackExchange.helpers.addSpinner(infoDiv[0]);
getRedirectUrl(`${location.origin}` + url).then(function (v) {
const rid = v.match(/\d+$/)[0];
$.post(`${location.origin}/review/next-task/` + rid, { 'taskTypeId': 1, 'fkey': fkey }, function (data) {
StackExchange.helpers.removeSpinner();
infoDiv.append(`<div class="review-instructions infobox">${data.instructions}</div>`).append(`<div class="review-more-instructions">${data.moreInstructions}</div>`);
const spamRejects = data.instructions.match(/This edit defaces/g);
if (spamRejects && spamRejects.length > 0) {
infoDiv.before(`, for <b>spam/vandalism</b> x${spamRejects.length}`);
}
const drasticRejects = data.instructions.match(/This edit deviates from the original intent/g);
if (drasticRejects && drasticRejects.length > 0) {
infoDiv.before(`, for <b>drastic changes</b> x${drasticRejects.length}`);
}
const harmfulRejects = data.instructions.match(/completely superfluous or actively harm readability/g);
if (harmfulRejects && harmfulRejects.length > 0) {
infoDiv.before(`, for <b>no improvement</b> x${harmfulRejects.length}`);
}
const tagRejects = data.instructions.match(/This edit introduces tags that do not help to define the topic/g);
if (tagRejects && tagRejects.length > 0) {
infoDiv.before(`, for <b>irrelevant tags</b> x${tagRejects.length}`);
}
const replyRejects = data.instructions.match(/should have been written as a comment/g);
if (replyRejects && replyRejects.length > 0) {
infoDiv.before(`, for <b>attempting to reply</b> x${replyRejects.length}`);
}
});
});
}
return false;
});
}
// Append styles
addStylesheet(`
#reviews {
line-height: 2em;
font-size: 14px;
}
.review:nth-child(even) {
background: var(--black-025);
}
.review .toggle {
content: '';
display: inline-block;
width: 0;
height: 0;
border-style: solid;
border-width: 6px 0 6px 9px;
border-color: transparent transparent transparent var(--black-400);
transform-origin: center;
}
.review .review-info {
display: none;
}
.review.open .toggle {
transform: rotateZ(90deg);
}
.review.open .review-info {
display: block !important;
}
.review .userspan {
display: inline-block;
min-width: 230px;
text-align: right;
}
.review .userlink {
display: inline-block;
}
span.userlink {
font-style: italic;
}
.review .answer-hyperlink {
display: inline-block;
min-width: 71px;
margin-bottom: 0;
}
.review > .relativetime {
display: inline-block;
}
.review .relativetime:not([title$='Z']):before {
content: 'at ';
}
.review.review-bar-container {
margin-top: 0;
}
.review-bar-container .review-bar {
line-height: 1;
font-size: 1rem;
}
.review .ajax-loader {
margin: 15px;
}
.review-instructions {
margin-left: 14px;
margin-top: 10px;
}
.review-bar-container .review-bar .review-more-instructions {
padding: 16px 14px 0px;
}
.pager, .toggle {
user-select: none;
}
`); // end stylesheet
// On script run
(function init() {
if (location.pathname === '/review/suggested-edits/history/rejected') {
appendStyles();
initRejectsPage();
}
else if (location.pathname.includes('/review/suggested-edits')) {
// Add to nav tabs
const histlink = $('#tabs').find('a[href="/review/suggested-edits/history"]');
histlink.clone().removeClass('youarehere').attr('href', '/review/suggested-edits/history/rejected').text('rejects').insertAfter(histlink);
}
else if (location.pathname === '/admin/links') {
$('.content-page ul').first().append('<li><a href="/review/suggested-edits/history/rejected">Rejected suggested edits</a></li>');
}
})();