-
Notifications
You must be signed in to change notification settings - Fork 34
/
RedactedScreenshots.user.js
195 lines (154 loc) · 6.41 KB
/
RedactedScreenshots.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
// ==UserScript==
// @name Redacted Screenshots
// @description Masks and hides user identifying info
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 3.0.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 */
/// <reference types="./globals" />
'use strict';
const ipRegex = /(\d{1,3})(\.\d{1,3}){3}(?=(?:\b|"|'))/g;
const emailRegex = /([^@\s]{1,3})([^@\s]+)@(\S+)\.([a-z]+)(?=(?:\b|"|'))/gi;
function redactPii(i, elem) {
if (!(ipRegex.test(elem.innerHTML) || emailRegex.test(elem.innerHTML)) || $(this).find('*').length > 10) return;
elem.innerHTML = elem.innerHTML
.replace(ipRegex, '$1.███.███$2 ')
.replace(emailRegex, '$1██████@██████.$4');
}
function cleanPage() {
// Reset UI (indication of votes/fav)
$('.vote-up-on, .vote-down-on').removeClass('vote-up-on vote-down-on');
$('.star-on').removeClass('star-on');
$('.favoritecount-selected').removeClass('favoritecount-selected');
// Remove/Reset other SOMU items
$('body').removeClass('usersidebar-open');
$('.old-comment, .cmmt-rude, .cmmt-chatty').removeClass('old-comment cmmt-rude cmmt-chatty');
$('#usersidebar, #qtoc, .meta-mentioned, .post-stickyheader, .dissociate-post-link, .post-id, .js-better-inline-menu, .print-comment-buttons').remove();
// Remove other userscript items
$('#roombaTableDiv').remove();
// Remove unnecessary stuff from page
$('.my-profile, .user-gravatar32 a, .user-info .-flair').remove();
// Remove admin stuff from page
$('.js-post-issue, .js-post-issues, .js-mod-inbox-button, .flag-count-item, .delete-comment, .js-post-flag-bar').remove();
// Redact IP and email addresses in content div
const content = $('#content');
$('input, a', content).each(redactPii);
$('.s-prose li, .s-prose p, #annotations .pre', content).each(redactPii);
$('div > div, div > p, div > span', content).each(redactPii);
// Shrink user comments table for mod messages
$('table.admin-user-comments').css({ width: '720px' });
}
function anonymizeUsers(fullwipe = false) {
// Wrap spans around question closers text in close reasons so we can process those too
$('.close-as-off-topic-status-list li').each(function () {
if (this.innerHTML.includes('–') && !this.innerHTML.includes('js-reason-user')) {
let parts = this.innerHTML.split('–');
this.innerHTML = parts[0] + '–' + parts[1].replace(/(,? )([\w\s]+)/g, '$1<span class="js-reason-user">$2</span>');
}
});
const dataSet = [];
let usernum = 1;
// Anonymize userlinks in these sections only...
const $sections = $('#mod-content, #content, .admin-user-comments, h1');
// Get user links
const userlinks = $('a[href*="/users/"]', $sections).each(function (i, el) {
const username = this.innerText.trim();
const match = this.href.match(/.*\/users\/-?(\d+)(\/.*)?/);
// Does not match valid user URL, ignore this link
if (!match) return;
// Map user id to unique value
const uid = match[1];
if (!dataSet[uid]) {
dataSet[uid] = usernum++;
}
// Set user link text
// If not full redact, also keep mod diamond in comments
const isComment = $(this).closest('.comment-body').length > 0;
const isMod = this.innerText.includes('♦') || $(this).next('.mod-flair').length !== 0;
const modFlair = isMod && isComment ? ' ♦' : '';
this.innerText = fullwipe ? "anon" : (isMod ? "mod" + modFlair : "anon-" + dataSet[uid]);
// Also find same username in question close reasons
$('.close-as-off-topic-status-list .js-reason-user').filter((i, el) => el.innerText == username).text(this.innerText);
});
// Edge case: CW posts that have no revision history
$('.post-signature .community-wiki').closest('.user-info').find('a[href$="/revisions"]').filter(function () {
return $(this).siblings('a[href^="/users/"]').length == 0;
}).text('anon');
// Remove @ replies from beginning of comments
$('.comment-copy, .text-row > td > span').html((i, v) => v.replace(/^@[\wŒŠŽÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞßðÿ]+[,:]?\s/i, ''));
// If fullwipe, also add class to body to further remove unneeded elements on page
$('body').addClass(fullwipe ? 'js-redactfull' : 'js-redactsemi');
}
// Append styles
addStylesheet(`
.redact-buttons {
position: fixed !important;
bottom: 3px;
left: 3px;
z-index: 999999;
}
.redact-buttons:hover button ~ button {
display: inline-block;
}
.redact-buttons button {
opacity: 0.5;
}
.redact-buttons button:hover {
opacity: 1;
}
.redact-buttons button ~ button {
display: none;
margin-left: 3px;
}
body.js-redactfull .mod-flair,
body.js-redactfull .js-usercolor:after {
display: none !important;
}
body.js-redactfull .user-details a,
body.js-redactfull a.comment-user {
color: var(--black-400);
}
body.js-redactsemi.question-page #answers .answer,
body.js-redactfull.question-page #answers .answer {
border-bottom: 1px solid var(--black-075);
}
.post-signature .user-info {
min-height: 67px;
}
`); // end stylesheet
// On script run
(function init() {
const redactButtons = $(`<div class="redact-buttons"><button>Redact</button><button data-fullwipe="true">Redact Full</button></div>`).appendTo('body');
redactButtons.on('click', 'button', function (evt) {
const isFullWipe = !!evt.target.dataset.fullwipe;
cleanPage();
anonymizeUsers(isFullWipe);
// Hide buttons for 10 seconds to allow for screenshot taking
redactButtons.hide().delay(10000).fadeIn(1);
// If full wipe, also remove other button
if (isFullWipe) {
$(this).siblings().remove();
}
});
})();