-
Notifications
You must be signed in to change notification settings - Fork 34
/
CommentUndeleter.user.js
127 lines (108 loc) · 3.89 KB
/
CommentUndeleter.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
// ==UserScript==
// @name Comment Undeleter
// @description Allows moderators to undelete user-deleted comments
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 3.1.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/*
//
// @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';
// This is a moderator-only userscript
if (!isModerator()) return;
// Delete individual comment
function deleteComment(cid) {
return new Promise(function (resolve, reject) {
if (hasInvalidIds(cid)) { reject(); return; }
$.post({
url: `${location.origin}/posts/comments/${cid}/vote/10`,
data: {
'fkey': fkey,
'sendCommentBackInMessage': true
}
})
.done(function (json) {
if (json.Success) $('#comment-' + cid).replaceWith(json.Message);
resolve(cid);
})
.fail(reject);
});
}
// Undelete individual comment
function undeleteComment(pid, cid) {
return new Promise(function (resolve, reject) {
if (hasInvalidIds(pid, cid)) { reject(); return; }
$.post({
url: `${location.origin}/admin/posts/${pid}/comments/${cid}/undelete`,
data: { 'fkey': fkey }
})
.done(function (data) {
const cmmt = $('#comment-' + cid).replaceWith(data);
cmmt[0].dataset.postId = pid;
resolve(cid);
})
.fail(reject);
});
}
// Add undelete link to deleted comment info if not found
function addUndeleteButtons() {
$('.deleted-comment-info').filter(function () {
return $(this).children('.js-comment-undelete').length == 0;
}).append(`<button class="js-comment-undelete undelete-comment s-btn s-btn__link">Undelete</button>`);
}
// Append styles
addStylesheet(`
/* Show undelete link even on deleted answers */
.deleted-answer .comments .deleted-comment .undelete-comment,
.deleted-answer .comments .deleted-comment:hover .undelete-comment {
display: inline-block;
}
`); // end stylesheet
// On script run
(function init() {
// Only on mod flag queue "commentvandalismdeletionsauto"
if (isModDashboardPage && location.search.includes('flagtype=commentvandalismdeletionsauto')) {
// Add global event for undelete button clicks
$(document).on('click', '.js-comment-undelete', function () {
const cmmt = $(this).closest('.comment');
const pid = cmmt[0].dataset.postId ?? cmmt.closest('.comments')[0]?.id.split('-')[1];
const cid = cmmt.attr('id').split('-')[1];
undeleteComment(pid, cid);
return false;
});
// Add global event for delete button clicks
$(document).on('click', '.js-comment-delete', function () {
const cmmt = $(this).closest('.comment');
const pid = cmmt[0].dataset.postId ?? cmmt.closest('.comments')[0]?.id.split('-')[1];
const cid = cmmt.attr('id').split('-')[1];
deleteComment(cid).then(() => {
// Set post id to comment so we can undelete
cmmt[0].dataset.postId = pid;
});
return false;
});
}
$(document).ajaxStop(function (event, xhr, settings) {
setTimeout(addUndeleteButtons, 100);
});
})();