-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
hide-useless-comments.tsx
67 lines (58 loc) · 2.14 KB
/
hide-useless-comments.tsx
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
import './hide-useless-comments.css';
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
function init(): void {
let uselessCount = 0;
for (const commentText of select.all('.comment-body > p:only-child')) {
// Find useless comments
if (!/^([+-]\d+!*|👍|🙏|👎|👌|)+$/.test(commentText.textContent!.trim())) {
continue;
}
// Comments that contain useful images shouldn't be removed
if (select.exists('a img', commentText)) {
continue;
}
// Ensure that they're not by VIPs (owner, collaborators, etc)
const comment = commentText.closest<HTMLElement>('.js-timeline-item')!;
if (select.exists('.timeline-comment-label', comment)) {
continue;
}
// If the person is having a conversation, then don't hide it
const author = select('.author', comment)!.getAttribute('href');
// If the first comment left by the author isn't a useless comment
// (previously hidden or about to be hidden), then leave this one as well
const previousComment = select(`.js-timeline-item:not([hidden]) .unminimized-comment .author[href="${author}"]`);
if (previousComment && previousComment.closest('.js-timeline-item') !== comment) {
continue;
}
comment.hidden = true;
comment.classList.add('rgh-hidden-comment');
uselessCount++;
}
if (uselessCount > 0) {
select('.discussion-timeline-actions')!.prepend(
<p className="rgh-useless-comments-note">
{`${uselessCount} unhelpful comment${uselessCount > 1 ? 's were' : ' was'} automatically hidden. `}
<button className="btn-link text-emphasized" onClick={unhide}>Show</button>
</p>
);
}
}
function unhide(event: React.MouseEvent<HTMLButtonElement>): void {
for (const comment of select.all('.rgh-hidden-comment')) {
comment.hidden = false;
}
select('.rgh-hidden-comment')!.scrollIntoView();
event.currentTarget.parentElement!.remove();
}
features.add({
id: __featureName__,
description: 'Hides reaction comments ("+1", "👍", …).',
screenshot: 'https://user-images.githubusercontent.com/1402241/45543717-d45f3c00-b847-11e8-84a5-8c439d0ad1a5.png',
include: [
features.isIssue
],
load: features.onAjaxedPages,
init
});