-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathChatRedactMessages.user.js
127 lines (106 loc) · 4.22 KB
/
ChatRedactMessages.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 Chat Redact Messages
// @description Add "Redact + Purge + Delete" button to message history page
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 4.0.13
//
// @match https://chat.stackoverflow.com/*
// @match https://chat.stackexchange.com/*
// @match https://chat.meta.stackexchange.com/*
//
// @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, store, fkey */
/// <reference types="./globals" />
'use strict';
// This is a moderator-only userscript
if (!isModerator()) return;
const storeKey = `fkey-${location.hostname}`;
let cachedfkey = store.getItem(storeKey);
let redactText = `[message redacted by moderator]`;
// Append styles
addStylesheet(`
#redactpurge {
position: relative;
top: -2px;
}
`); // end stylesheet
// On script run
(function init() {
// On message history page
if (/^\/messages\/\d+\/history$/.test(location.pathname) && cachedfkey != null) {
const mid = Number(location.pathname.replace(/[^\d]+/g, ''));
const header = document.getElementById('content').querySelector('h2');
const currMessage = document.querySelector('.message > .content').innerText;
const isDeleted = header.innerText.includes('deletion');
const isRedacted = currMessage === redactText || currMessage.includes('redact');
// If already redacted, do nothing
if (isRedacted) return;
// Add redact button next to header
header.insertAdjacentHTML('afterend', '<input class="button" type="submit" value="Redact + Purge + Delete" id="redactpurge">');
const redactBtn = document.getElementById('redactpurge');
// Click event on button
redactBtn.addEventListener('click', evt => {
// Disable button to prevent double-clicking
evt.target.disabled = true;
let form;
// Delete if not already deleted
if (!isDeleted) {
form = new FormData();
form.append('fkey', cachedfkey);
fetch(location.origin + '/messages/' + mid + '/delete', {
method: 'POST',
body: form
});
}
// Edit to replace message with redactText
form = new FormData();
form.append('fkey', cachedfkey);
form.append('text', '*' + redactText + '*');
fetch(location.origin + '/messages/' + mid, {
method: 'POST',
body: form
}).then(() => {
// Purge history
form = new FormData();
form.append('fkey', cachedfkey);
fetch(location.origin + '/messages/' + mid + '/purge-history', {
method: 'POST',
body: form
}).then(() => {
// Refresh page to reflect changes
location.reload();
});
});
});
}
// Other chat pages
else {
// Cache fkey for use in message history page
const newfkey = typeof _window.fkey === 'function' ? _window.fkey().fkey : document.querySelector('#fkey').value;
if (newfkey) store.setItem(storeKey, newfkey);
// When message actions link is clicked
document.querySelectorAll('#chat .action-link, #transcript .action-link').forEach(elm => {
elm.addEventListener('click', evt => {
const message = evt.target.closest('.message');
const mid = message.id.replace(/[^\d]+/g, '');
// wait for the DOM to add the popup element
waitForElem('.popup', message).then(popup => {
popup = popup[0];
const histlink = popup.querySelector('a[href$=history]');
// If no history link in popup, insert history link after permalink
if (!histlink) {
const permalink = popup.querySelector('a[href^="/transcript/message/"]');
permalink.innerText = 'link';
permalink.parentElement.lastElementChild.remove(); // remove <br>
permalink.parentElement.insertAdjacentHTML('beforeend', '; <a href="/messages/' + mid + '/history" title="view and redact message history">history</a><br>');
} else {
histlink.title = 'view and redact message history';
}
});
});
});
}
})();