/
notification-filter.user.js
294 lines (252 loc) · 9.16 KB
/
notification-filter.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// ==UserScript==
// @name Devrant Notification Filter
// @namespace https://devrant.com/
// @version 0.8
// @description Add category filters to notifications
// @author 7twin
// @match https://devrant.com/notifs*
// @match https://devrant.com/rants/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addValueChangeListener
// @grant GM_addStyle
// @run-at document-start
// ==/UserScript==
// ------ config ------
var show_read = false; // show read notifications in other tabs than "all"?
var default_unread_filter = true; // should the unread page be default shown?
// ------ config end ------
// current filter needle to search for
var filter = ((default_unread_filter === true) ? "unread" : false);
// unread states
var plusplus_unread = false;
var mentions_unread = false;
var comments_unread = false;
var subs_unread = false;
var me_unread = false;
function reset_unread(){
plusplus_unread = false;
mentions_unread = false;
comments_unread = false;
subs_unread = false;
me_unread = false;
}
function set_unread_states(){
if(plusplus_unread === true || mentions_unread === true || comments_unread === true || subs_unread === true || me_unread === true){
$("#all_link").css("color","#7CC8A2");
$("#unread_link").css("color","#7CC8A2");
}else{
$("#all_link").css("color","rgba(255,255,255,0.7)");
$("#unread_link").css("color","rgba(255,255,255,0.7)");
}
if(plusplus_unread === true){
$("#plusplus_link").css("color","#7CC8A2");
}else{
$("#plusplus_link").css("color","rgba(255,255,255,0.7)");
}
if(mentions_unread === true){
$("#mentions_link").css("color","#7CC8A2");
}else{
$("#mentions_link").css("color","rgba(255,255,255,0.7)");
}
if(comments_unread === true){
$("#comments_link").css("color","#7CC8A2");
}else{
$("#comments_link").css("color","rgba(255,255,255,0.7)");
}
if(subs_unread === true){
$("#subs_link").css("color","#7CC8A2");
}else{
$("#subs_link").css("color","rgba(255,255,255,0.7)");
}
if(me_unread === true){
$("#me_link").css("color","#7CC8A2");
}else{
$("#me_link").css("color","rgba(255,255,255,0.7)");
}
}
function check_unread(notification){
if(notification.closest("li").hasClass("notif-new")){
if(notification.text().indexOf("++'d your comment") !== -1 || notification.text().indexOf("++'d your rant") !== -1){
plusplus_unread = true;
}
if(notification.text().indexOf("mentioned you in a comment") !== -1){
mentions_unread = true;
}
if(notification.text().indexOf("posted a new rant") !== -1){
subs_unread = true;
}
if(notification.text().indexOf("commented on your rant") !== -1 || notification.text().indexOf("mentioned you in a comment") !== -1){
me_unread = true;
}
if(notification.text().indexOf("comments on a rant you commented on") !== -1 || notification.text().indexOf("commented on your rant") !== -1){
comments_unread = true;
}
}
}
function filter_notifications(){
reset_unread();
$("span.notif-body-text").each(function(){
check_unread($(this));
if(filter !== false){
if(filter === "unread" && $(this).closest("li").hasClass("notif-new")){
$(this).closest("li").show();
}else if(filter === "me" && $(this).closest("li").hasClass("notif-new") && ($(this).text().indexOf("commented on your rant") !== -1 || $(this).text().indexOf("mentioned you in a comment") !== -1)){
$(this).closest("li").show();
}else if($(this).text().indexOf(filter) !== -1 && filter !== "me"){
if(show_read === false && !$(this).closest("li").hasClass("notif-new") && filter !== "all"){
$(this).closest("li").hide();
}else{
$(this).closest("li").show();
}
}else{
$(this).closest("li").hide();
}
}else{
$(this).closest("li").show();
}
});
set_unread_states();
}
function override_hyperlinks(){
$('a.notif-link').off('click').on('click', function(e) {
if($(this).attr("href").indexOf("/rants/") !== -1){
e.preventDefault();
if(filter === "unread"){
$(this).closest("li").hide();
}
$(this).closest("li").removeClass("notif-new");
filter_notifications();
window.open($(this).attr("href"), '_blank');
}
});
}
function set_active_filter_badge(filter_index){
$(".notif-selected").removeClass("notif-selected");
if(filter_index === "all"){
$("#all_link").addClass("notif-selected");
}else if(filter_index === "plusplus"){
$("#plusplus_link").addClass("notif-selected");
}else if(filter_index === "mentions"){
$("#mentions_link").addClass("notif-selected");
}else if(filter_index === "comments"){
$("#comments_link").addClass("notif-selected");
}else if(filter_index === "subs"){
$("#subs_link").addClass("notif-selected");
}else if(filter_index === "unread"){
$("#unread_link").addClass("notif-selected");
}else if(filter_index === "me"){
$("#me_link").addClass("notif-selected");
}
}
function set_active_filter(filter_index){
if(filter_index === "all"){
filter = false;
}else if(filter_index === "plusplus"){
filter = "++'d";
}else if(filter_index === "mentions"){
filter = "mentioned you in a comment";
}else if(filter_index === "comments"){
filter = "comments on a rant you commented on";
}else if(filter_index === "subs"){
filter = "posted a new rant";
}else if(filter_index === "unread"){
filter = "unread";
}else if(filter_index === "me"){
filter = "me";
}
set_active_filter_badge(filter_index);
filter_notifications();
}
function bind_filter_links(){
$('#all_link').on('click', function(e) {
e.preventDefault();
set_active_filter("all");
});
$('#unread_link').on('click', function(e) {
e.preventDefault();
set_active_filter("unread");
});
$('#plusplus_link').on('click', function(e) {
e.preventDefault();
set_active_filter("plusplus");
});
$('#mentions_link').on('click', function(e) {
e.preventDefault();
set_active_filter("mentions");
});
$('#comments_link').on('click', function(e) {
e.preventDefault();
set_active_filter("comments");
});
$('#subs_link').on('click', function(e) {
e.preventDefault();
set_active_filter("subs");
});
$('#me_link').on('click', function(e) {
e.preventDefault();
set_active_filter("me");
});
}
function inject_filters(){
var notifTypes = `
<div class="notif-types">
<a href="#" `+((default_unread_filter === false) ? 'class="notif-selected"' : '')+` id="all_link">All</a>
<a href="#" `+((default_unread_filter === true) ? 'class="notif-selected"' : '')+` id="unread_link">Unread</a>
<a href="#" id="me_link">Me</a>
<a href="#" id="plusplus_link">++'s</a>
<a href="#" id="mentions_link">@'s</a>
<a href="#" id="comments_link">Comments</a>
<a href="#" id="subs_link">Subs</a>
</div>`;
$(".notifs-tabs:first").html($(".notifs-tabs:first").html() + notifTypes);
}
function GM_onMessage(label, callback) {
GM_addValueChangeListener(label, function() {
callback.apply(undefined, arguments[2]);
});
}
function GM_sendMessage(label) {
GM_setValue(label, Array.from(arguments).slice(1));
}
function init_broadcast_listener(){
GM_onMessage('_.unique.opened-rant', function(src, message) {
if(src === "rant_read" && message.indexOf("https://devrant.com/rants/") !== -1){
$("a.notif-link").each(function(){
if(message.indexOf($(this).attr("href")) !== -1){
if($(this).closest("li").hasClass("notif-new")){
$(this).closest("li").removeClass("notif-new");
if(filter === "unread"){
$(this).closest("li").fadeOut(600, function() { $(this).closest("li").hide(); })
}
}
}
});
filter_notifications();
}
});
}
(function() {
'use strict';
GM_addStyle(`
.notif-selected{
border-bottom: 1px solid !important;
font-weight:100 !important;
}
.notif-types a{
padding-right:0 !important;
margin-right:10px;
}
`);
init_broadcast_listener();
inject_filters();
bind_filter_links();
$(document).ajaxStop(function () {
override_hyperlinks();
filter_notifications();
});
// broadcast rant to mark as viewed on notification page
if(window.location.href.indexOf("https://devrant.com/rants/") !== -1){
GM_sendMessage('_.unique.opened-rant', 'rant_read', window.location.href);
}
})();