-
Notifications
You must be signed in to change notification settings - Fork 34
/
ModBatchCommentDeleter.user.js
456 lines (419 loc) · 12.5 KB
/
ModBatchCommentDeleter.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
456
// ==UserScript==
// @name Mod Batch Comment Deleter
// @description Batch delete comments using comment IDs or permalinks (e.g.: from SEDE https://data.stackexchange.com/stackoverflow/query/1131935)
// @homepage https://github.com/samliew/personal-userscripts
// @author Samuel Liew
// @version 4.0.13
//
// @match https://*.stackoverflow.com/admin/deleter
// @match https://*.serverfault.com/admin/deleter
// @match https://*.superuser.com/admin/deleter
// @match https://*.askubuntu.com/admin/deleter
// @match https://*.mathoverflow.net/admin/deleter
// @match https://*.stackapps.com/admin/deleter
// @match https://*.stackexchange.com/admin/deleter
// @match https://stackoverflowteams.com/c/*/admin/deleter
//
// @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, fkey */
/// <reference types="./globals" />
'use strict';
const params = {
itemsPerBatch: 1000,
delayPerBatch: 5000,
};
let content, button, preview, textarea;
let isRunning = false;
let failures = 0, retryCount = 0;
// In case the batch endpoint is removed, we can use this to delete one by one
function deleteOne(url, callback = null) {
$.ajax({
url: url,
method: 'POST',
data: {
fkey: fkey,
sendCommentBackInMessage: false
},
dataType: 'json',
}).done(function (response) {
if (response.success == false) {
failures++;
}
if (typeof callback == 'function') callback();
}).fail(function (jqXHR, textStatus, errorThrown) {
failures++;
});
}
// Bulk delete a batch of 1000 comments
function bulkDeleteComments(commentIds) {
return new Promise(function (resolve, reject) {
if (typeof commentIds === 'undefined' || commentIds.length === 0) { reject(); return; }
const datastring = 'commentIds%5B%5D=' + commentIds.join('&commentIds%5B%5D=') + '&action=delete&fkey=' + fkey;
$.post(`${location.origin}/admin/comment/bulk-comment-change`, datastring)
.done(function (response) {
if (response.includes('ok') === false) {
failures++;
}
}).fail(function () {
failures++;
});
});
}
// Process queue
let doDeleteAll = function () {
const startTime = new Date();
let linkElems = preview.find('a').hide();
//let links = linkElems.get().map(el => {
// if(!el.href.includes('/vote/10')) { return el.href + '/vote/10' }
// return el.href;
//});
let commentIds = linkElems.get().map(el => el.href.match(/\/(\d+)/)[1]);
let total = typeof links !== 'undefined' ? links.length : commentIds.length;
let currentNum = 0;
linkElems.remove(); // Save memory?
// Calculate and update progress
function updateProgress() {
const minsElapsed = (Date.now() - startTime) / 60000;
const minsRemaining = (minsElapsed / currentNum) * (total - currentNum);
document.title = `${currentNum.toLocaleString()}/${total.toLocaleString()} (${(currentNum / total * 100).toFixed(1)}%)`;
preview.text(document.title + `. ${Math.floor(minsElapsed)} mins elapsed, ${Math.round(minsRemaining)} mins remaining`);
}
// Callback
function processNextBatch() {
if (currentNum >= total) cleanup();
//links.slice(currentNum, currentNum + params.itemsPerBatch).forEach(v => { deleteOne(v); });
bulkDeleteComments(commentIds.slice(currentNum, currentNum + params.itemsPerBatch));
currentNum += params.itemsPerBatch;
if (currentNum > total) currentNum = total;
}
// After a batch of ajax calls complete
$(document).unbind('ajaxStop').ajaxStop(function () {
updateProgress();
// Some errors, wait and try up to 3 times
if (failures > 1) {
// Too many errors, stop
if (retryCount >= 10) {
cleanup(true);
return;
}
// Reset and try last batch again in 2 minutes
retryCount++;
failures = 0;
$('input[data-param-name="itemsPerBatch"]').val('20').trigger('change');
$('input[data-param-name="delayPerBatch"]').val('20').trigger('change');
currentNum -= params.itemsPerBatch;
setTimeout(processNextBatch, 300000);
}
// Continue next batch after a delay
else {
setTimeout(processNextBatch, params.delayPerBatch);
}
});
// Begin
isRunning = true;
processNextBatch();
// Confirm when leaving page if batch script running
$(window).on('beforeunload', function (evt) {
return isRunning && confirm('Are you sure you wish to leave this page?');
});
// Stop everything and exit
function cleanup(hasError = false) {
isRunning = false;
$(document).unbind('ajaxStop');
$(window).off('beforeunload');
button.remove();
const endTime = new Date();
preview.text(hasError ?
`Receiving response errors. Check if you are rate-limited or these items are already deleted. Stopped at ${currentNum - params.itemsPerBatch} items.` :
`Completed ${total} items in ${Math.round((endTime - startTime) / 60000)} minutes!`
);
document.title = hasError ? 'Error!' : 'Completed!';
}
// for safety, doDeleteAll can be called once only
doDeleteAll = function () { };
}
// Update params from input values
function parseInputUpdatePreview(evt) {
// Do nothing and wait for another event
if (this.value.trim() == '') {
$(this).one('change keyup', parseInputUpdatePreview);
return false;
}
$(this).hide();
// HTML pasted from SEDE
if (this.value.includes('grid-canvas')) {
preview.html(this.value);
}
// Links pasted from CSV
else {
preview.html(`<a href="//${location.hostname}/` + this.value.replace(/[\s\n\r]+/g, ' ').trim().split(/\s?site:\/\//).join(`" target="_blank">link</a> <a href="//${location.hostname}/`) + `" target="_blank">link</a>`);
preview.children().first().remove();
}
// Show delete all button
button.insertBefore(preview).text('Delete ALL ' + preview.find('a').length.toLocaleString());
}
// Append styles
addStylesheet(`
#content {
display: block !important;
}
.slick-header.ui-state-default,.slick-headerrow.ui-state-default {
width: 100%;
overflow: hidden;
border-left: 0;
}
.slick-header-columns,.slick-headerrow-columns {
position: relative;
white-space: nowrap;
cursor: default;
overflow: hidden;
}
.slick-header-column.ui-state-default {
position: relative;
display: inline-block;
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
height: 16px;
line-height: 16px;
margin: 0;
padding: 4px;
border-right: 1px solid var(--silver);
border-left: 0;
border-top: 0;
border-bottom: 0;
float: left;
}
.slick-headerrow-column.ui-state-default {
padding: 4px
}
.slick-header-column-sorted {
font-style: italic;
}
.slick-sort-indicator {
display: inline-block;
width: 8px;
height: 5px;
margin-left: 4px;
margin-top: 6px;
}
.slick-sort-indicator-desc {
background: url(/Content/slickgrid/images/sort-desc.gif);
}
.slick-sort-indicator-asc {
background: url(/Content/slickgrid/images/sort-asc.gif);
}
.slick-resizable-handle {
position: absolute;
font-size: .1px;
display: block;
cursor: col-resize;
width: 4px;
right: 0;
top: 0;
height: 100%;
}
.slick-sortable-placeholder {
background: var(--silver);
}
.grid-canvas {
position: relative;
outline: 0;
}
.slick-row.ui-widget-content,.slick-row.ui-state-active {
position: absolute;
border: 0;
width: 100%;
}
.slick-cell,.slick-headerrow-column {
position: absolute;
border: 1px solid transparent;
border-right: 1px dotted var(--silver);
border-bottom-color: var(--silver);
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
vertical-align: middle;
z-index: 1;
padding: 1px 2px 2px 1px;
margin: 0;
white-space: nowrap;
cursor: default;
}
.slick-group {
}
.slick-group-toggle {
display: inline-block;
}
.slick-cell.highlighted {
background: #87cefa;
background: rgba(0,0,255,.2);
-webkit-transition: all .5s;
-moz-transition: all .5s;
-o-transition: all .5s;
transition: all .5s;
}
.slick-cell.flashing {
border: 1px solid var(--red-500) !important;
}
.slick-cell.editable {
z-index: 11;
overflow: visible;
background: var(--white);
border-color: var(--black);
border-style: solid;
}
.slick-cell:focus {
outline: none;
}
.slick-reorder-proxy {
display: inline-block;
background: var(--blue-500);
opacity: .15;
filter: alpha(opacity=15);
cursor: move;
}
.slick-reorder-guide {
display: inline-block;
height: 2px;
background: var(--blue-500);
opacity: .7;
filter: alpha(opacity=70);
}
.slick-selection {
z-index: 10;
position: absolute;
border: 2px dashed #000;
}
.tab-counter {
color: #b0b0b0;
display: none;
margin-left: 4px;
}
.youarehere .tab-counter:hover {
color: var(--blue-400);
}
#resultSets {
position: relative;
}
#resultSets .subpanel {
cursor: default;
display: none;
min-height: 500px;
}
.slick-header .slick-header-column {
background-color: var(--black-025);
color: var(--black-500);
border-bottom: 1px dotted var(--black-150);
display: block;
font-weight: bold;
padding: 6px 8px 5px 8px;
text-align: center;
}
.slick-header .slick-header-column-sorted {
color: var(--blue-500);
font-style: normal;
}
.slick-sort-indicator {
background-image: url(/Content/slickgrid/images/sort-asc-inactive.gif);
background-repeat: no-repeat;
height: 7px;
}
.slick-sort-indicator.slick-sort-indicator-asc {
background-image: url(/Content/slickgrid/images/sort-asc.gif);
}
.slick-sort-indicator.slick-sort-indicator-desc {
background-image: url(/Content/slickgrid/images/sort-desc.gif);
}
.slick-row.odd {
background-color: var(--black-025);
}
.slick-cell {
padding: 6px 8px 5px 8px;
border-bottom: 1px dotted var(--black-150);
border-right: 1px dotted var(--black-150);
}
.slick-cell.number {
text-align: right;
}
.grid-canvas {
display: table;
position: relative !important;
width: auto !important;
height: auto !important;
}
.slick-row {
display: table-row;
position: relative !important;
}
.slick-cell,
.slick-header-column {
display: table-cell;
position: relative !important;
max-width: 450px;
width: auto !important;
height: auto !important;
}
.slick-header-columns {
display: table;
position: relative !important;
left: 0 !important;
top: 0;
width: auto !important;
}
#mainbar-full + button {
margin-top: 20px;
}
.html-editor {
width: 100%;
height: 150px;
margin: 20px auto;
}
.html-preview {
margin: 20px auto;
}
input.inline {
width: 100px;
padding: 4px 7px !important;
border: none !important;
border-bottom: 1px dotted var(--black-700) !important;
}
`); // end stylesheet
// On script run
(function init() {
document.title = "Batch Comment Deleter - " + StackExchange.options.site.name;
// Init UI
content = $('#content').empty().prepend(`
<div id="mainbar-full">
<div class="grid ai-center jc-space-between mb12 bb bc-black-3 pb12">
<div class="fs-body3 flex--item fl1 mr12">Batch Comment Deleter</div>
</div>
<div class="deleter-info">items per batch: <input type="number" min="1" max="${Math.max(100, params.itemsPerBatch)}" class="inline" data-param-name="itemsPerBatch" value="${params.itemsPerBatch}" />; secs delay between batches: <input type="number" min="0" max="60" class="inline" data-param-name="delayPerBatch" value="${params.delayPerBatch / 1000}" /></div>
</div>`);
button = $(`<button>Delete ALL</button>`).on('click', function () {
$(this).prop('disabled', true).text('processing...');
doDeleteAll();
});
preview = $(`
<div class="html-preview">
Use this <a href="https://data.stackexchange.com/stackoverflow/query/1131935" target="_blank">SEDE query</a> to find comments to delete. Download results and paste the comment permalinks column below.
</div>`).appendTo(content);
textarea = $('<textarea placeholder="paste comment permalinks from exported query" class="html-editor"></textarea>').appendTo(content).one('change keyup', parseInputUpdatePreview);
// Events to update params
content.on('change', 'input[data-param-name]', function (evt) {
const paramName = this.dataset.paramName;
const num = Number(this.value) * 1000;
const isNum = !isNaN(num);
if (this.value != '') params[paramName] = isNum ? num : this.value;
});
})();