-
Notifications
You must be signed in to change notification settings - Fork 34
/
DuplicateAnswersFlagsHelper.user.js
133 lines (109 loc) · 4.79 KB
/
DuplicateAnswersFlagsHelper.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
// ==UserScript==
// @name Duplicate Answers Flags Helper
// @description Add action button to delete AND insert duplicate comment at the same time
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 4.0.13
//
// @match https://*.stackoverflow.com/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://*.serverfault.com/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://*.superuser.com/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://*.askubuntu.com/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://*.mathoverflow.net/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://*.stackapps.com/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://*.stackexchange.com/admin/dashboard?flagtype=answerduplicateanswerauto*
// @match https://stackoverflowteams.com/c/*/admin/dashboard?flagtype=answerduplicateanswerauto*
//
// @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
// @require https://raw.githubusercontent.com/samliew/ajax-progress/master/jquery.ajaxProgress.js
// ==/UserScript==
/* globals StackExchange, fkey, scriptName */
/// <reference types="./globals" />
'use strict';
// This is a moderator-only userscript
if (!isModerator()) return;
const superusers = [584192];
const isSuperuser = superusers.includes(selfId);
let duplicateComment = `Please [don't post identical answers to multiple questions](https://meta.stackexchange.com/q/104227). Instead, tailor the answer to the question asked. If the questions are exact duplicates of each other, please vote/flag to close instead.`;
// Helper functions to wait for SOMU options to load
const rafAsync = () => new Promise(resolve => { requestAnimationFrame(resolve); });
const waitForSOMU = async () => {
while (typeof SOMU === 'undefined' || !SOMU?.hasInit) { await rafAsync(); }
return SOMU;
};
// Append styles
addStylesheet(`
#actionBtns {
margin: 25px 24px 20px;
}
#actionBtns button {
margin-top: 10px;
margin-right: 10px;
}
.rec-button {
padding: 3px 5px;
border: 1px solid var(--red-500) !important;
color: var(--red-500) !important;
}
.rec-button:hover {
background-color: var(--black-050);
}
`); // end stylesheet
// On script run
(async function init() {
// Remove convert to comment buttons
$('.convert-to-comment').remove();
$('.js-flagged-post').each(function () {
// Add delete and comment button
$('.js-post-flag-options .ff-row-wrap', this).append(`<input type="button" class="js-hide-on-delete flex--item s-btn s-btn__danger s-btn__outlined js-delete-and-comment" data-post-id="${this.dataset.postId}" value="Delete + Comment" title="delete and add dupe comment" />`);
})
.on('click', '.js-delete-and-comment', function () {
const pid = this.dataset.postId;
const $post = $(this).closest('.js-flagged-post');
// Delete post
$.post({
url: `https://stackoverflow.com/posts/${this.dataset.postId}/comments`,
data: {
'fkey': fkey,
'comment': duplicateComment
}
});
// Add comment
$.post({
url: `https://stackoverflow.com/posts/${this.dataset.postId}/vote/10`,
data: { 'fkey': fkey }
});
// Hide post immediately so we can move on
$(this).hide();
$post.hide();
});
const actionBtns = $('<div id="actionBtns"></div>');
$('.js-flagged-post').first().parent().prepend(actionBtns);
if (!isSuperuser) return;
// Delete + Comment ALL
$('<button class="s-btn s-btn__danger s-btn__filled s-btn__xs">Delete + Comment ALL</button>')
.appendTo(actionBtns)
.on('click', function () {
if (!confirm('Confirm Delete ALL?')) return false;
$(this).remove();
const visibleItems = $('.js-delete-and-comment:visible');
$('body').showAjaxProgress(visibleItems.length * 2, { position: 'fixed' });
visibleItems.trigger('click');
});
// Wait for options script to load
waitForSOMU().then(SOMU => {
// Set option field in sidebar with current custom value; use default value if not set before
SOMU.addOption(scriptName, 'Duplicate Comment', duplicateComment);
// Get current custom value with default
duplicateComment = SOMU.getOptionValue(scriptName, 'Duplicate Comment', duplicateComment);
});
})();