-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf-comment-sorter.user.js
65 lines (52 loc) · 1.95 KB
/
cf-comment-sorter.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
// ==UserScript==
// @name Codeforces comment sorter
// @grant none
// @include http://codeforces.com/blog/entry*
// @include http://codeforces.ru/blog/entry*
// @require https://raw.githubusercontent.com/lodash/lodash/3.3.1/lodash.min.js
// ==/UserScript==
(function () {
function print(o) {
console.log(JSON.stringify(o));
}
function voteSum(comment) {
var rating = comment.getElementsByClassName('commentRating')[0];
return parseInt(rating.children[0].innerHTML);
}
function getId(comment) {
return comment.getElementsByClassName('comment-table')[0].getAttribute('commentId');
}
function sortComments(element, parentId) {
var comments = _.filter(element.getElementsByClassName('comment'), function (comment) {
return !comment.classList.contains('comment-reply-prototype');
});
if (parentId !== '-1') {
comments = _.map(comments, function (comment) {
return comment.parentNode;
});
}
var topComments = _.filter(comments, function (comment) {
return comment.outerHTML.indexOf('commentparentid="' + parentId + '"') != -1;
});
if (!topComments.length) {
return;
}
var parentNode = topComments[0].parentNode;
_.forEachRight(topComments, function (comment) {
comment.parentNode.removeChild(comment);
});
topComments.sort(function (a, b) {
return voteSum(b) - voteSum(a);
});
_.forEach(topComments, function (comment) {
parentNode.appendChild(comment);
});
_.forEach(topComments, function (comment) {
var elementToSort = parentId === '-1' ? comment : comment.childNodes[2];
sortComments(elementToSort, getId(comment));
});
}
print('comment sorter started');
sortComments(document, '-1');
print('comment sorter finished');
})();