-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
104 lines (93 loc) · 3.18 KB
/
main.js
File metadata and controls
104 lines (93 loc) · 3.18 KB
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
var redrawGutter = () => {
const url = window.location.href;
var comments;
if (url.includes("news.ycombinator.com")) {
comments = $('tr.athing.comtr').map((_index, element) => ({
absoluteTop: $(element).offset().top,
top: $(element).offset().top / $(document).height(),
height: $(element).height() / $(document).height(),
depth: $(element).find('.ind').attr('indent'),
text: $(element).find('.commtext.c00').text()
})).get()
} else if (url.includes("github.com")) {
comments = $('.comment').map((_index, element) => ({
absoluteTop: $(element).offset().top - 59, // fit under thread sticky header
top: $(element).offset().top / $(document).height(),
height: $(element).height() / $(document).height(),
depth: 0,
text: $(element).find('.comment-body').text()
})).get();
} else { /*old.reddit.com*/
comments = $('.entry').map((_index, element) => ({
absoluteTop: $(element).offset().top,
top: $(element).offset().top / $(document).height(),
height: $(element).height() / $(document).height(),
depth: Math.max($(element).parents().length - 5, 0) / 3, // 5, 8, 11... to 0, 1, 2...
text: $(element).find('.md').text()
})).get();
}
$(".comment-gutter-overlay").remove();
comments.forEach(comment => {
const hasLinks = comment.text.includes('https://') || comment.text.includes('http://');
const isBig = comment.text.length > 1000; // including formatting
const isColorMarked = isBig || hasLinks;
const isTopLevel = comment.depth == 0;
const isViewed = isColorMarked || isTopLevel;
if (isViewed) {
var div = document.createElement("div");
div.className = "comment-gutter-overlay";
div.style.position = "fixed";
div.style.right = "0px";
div.style.top = comment.top * 100 + '%';
div.style.width = "8px";
div.style.height = Math.min(comment.height * 100, 2) + '%'; // capped at 2% window height
div.style.background = hasLinks ? "#08D" /*blue*/ : isBig ? "#3D0" /*green*/ : "#DDB" /*gray*/;
switch (Math.round(comment.depth)) {
case 0:
div.style.opacity = isColorMarked ? 1.0 : 0.8;
break;
case 1:
div.style.opacity = 1.0;
break;
case 2:
div.style.opacity = 0.8;
break;
case 3:
div.style.opacity = 0.6;
break;
default:
div.style.opacity = 0.5;
};
$(div).hover(() => {
$(div).css('cursor', 'pointer');
});
$(div).click(() => {
$('html, body').animate({
scrollTop: comment.absoluteTop
}, 200 /*millis*/);
});
document.body.appendChild(div);
}
});
};
$(document).on('pjax:end', () => {
redrawGutter();
});
var documentHeight = $(document).height();
$(window).bind('DOMSubtreeModified', () => {
setTimeout(() => {
if ($(document).height() != documentHeight) {
documentHeight = $(document).height();
redrawGutter();
}
}, 10 /*millis*/)
});
if (window.location.href.includes("github.com")) {
setTimeout(() => {
redrawGutter();
}, 500 /*millis*/)
}
$(window).resize(() => {
redrawGutter();
});
redrawGutter();