-
Notifications
You must be signed in to change notification settings - Fork 7
/
FBNotificationHighlighter
362 lines (318 loc) · 13 KB
/
FBNotificationHighlighter
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// ==UserScript==
// @name Facebook Notifications Highlighter
// @namespace http://www.JamesKoss.com/
// @version 2.58
// @description Highlights your interesting notifications, with an option to hide the unimportant ones.
// @author James Koss
// @match https://www.facebook.com/*
// @supportURL https://greasyfork.org/en/scripts/27189-facebook-your-notifications-highlighter/feedback
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
var first = true; // First notifications popup loading.
var hidden = false; // Non-highlighted notifications are hidden.
var updater = null; // Timed updating.
var fbNotificationsJewel = null;
var fbNotificationsFlyout = null;
var ulElement = null;
var firstNotification = null;
var lastNotification = null;
var notificationColors = {
'darkblue': '#c3d4ef',
'lightpink': '#efd0ea',
'lightgreen': '#c9f1c9',
'darkpurple': '#dec3ef'
};
// Substring in notification.
// Array must match all substrings in notification.
// NOTE: There's more general matches in updateNotifications().
var notificationTypes = {
'important': [
'replied to your',
'commented on your',
'commented on and reacted to your',
'shared your',
'mentioned you',
'tagged you',
"you're tagged in",
'also replied',
'also commented on his post',
'also commented on her post',
'also commented on their post',
['replied to', 'on your'],
['marked', 'safe'],
'replied to his'
],
'react': [
//'likes your',
//'like your',
//'liked your',
//'reacted to',
//['likes a', 'you']
],
'urgent': [
'your Timeline',
'added a photo of you',
'invited you to join',
'is live now',
'now a member of',
'on your timeline'
],
'mod': [
'approved your',
'changed the name of the',
'changed the type of the',
'needs review',
'flagged as possible spam',
'made you',
'changed the privacy'
]
};
// Should run after page has loaded, without CSS and images.
document.addEventListener('DOMContentLoaded', function() {
startScript();
}, false);
function startScript() {
// Listen to a click on the notifications button on FB.
fbNotificationsJewel = document.getElementById("fbNotificationsJewel");
// Don't reload script on Ajax page change only.
if (fbNotificationsJewel === null) return;
fbNotificationsJewel.addEventListener("click", onclickJewel);
fbNotificationsFlyout = document.getElementById('fbNotificationsFlyout');
// Auto updater.
updater = setInterval(updateView, 500);
}
// Clicking the notifications button on FB.
function onclickJewel(e) {
if (e.which === 1) {
setTimeout(function(){ updateNotifications(); }, 500);
}
}
// Check if notifications need updating while displayed.
function updateView() {
// Notifications must be visible and ready.
if (fbNotificationsFlyout.offsetParent === null) return;
if (ulElement === null) return;
// Request more notifications.
if (hidden === true) {
// Scrollbar hidden.
var sbh = fbNotificationsFlyout.querySelector('div.uiScrollableAreaGripper.hidden_elem');
// Or scrolled to bottom.
var wrap = fbNotificationsFlyout.querySelector('div.uiScrollableAreaWrap');
var bottom = wrap.offsetHeight + wrap.scrollTop >= wrap.scrollHeight - 10;
// And loading jewel must exist, otherwise finished all loadings.
var jewel = fbNotificationsFlyout.querySelector('div.uiScrollableAreaContent span.jewelLoading');
if ((sbh || bottom) && jewel) {
// Fake a scroll event for loader.
var e = new Event('scroll');
fbNotificationsFlyout.querySelector('div.uiScrollableArea').dispatchEvent(e);
}
}
var mergeLIs = ulElement.querySelectorAll("._33c");
var curFirstNotification = mergeLIs[0];
var curLastNotification = mergeLIs[mergeLIs.length-1];
// Update view, if new notifications, or loaded old ones.
if (curLastNotification !== lastNotification ||
curFirstNotification !== firstNotification) {
// Update globals.
firstNotification = curFirstNotification;
lastNotification = curLastNotification;
updateNotifications();
updateVisibility();
}
}
// After clicking the notification, remove highlight color and eventListener.
function removeHighlight(e) {
e.currentTarget.removeEventListener("click", removeHighlight);
// Remove script styling.
e.currentTarget.style.backgroundColor = "";
if (e.currentTarget.paramA !== null) {
e.currentTarget.paramA.style.backgroundColor = "";
}
}
// Update notifications visibility.
function updateVisibility(req) {
// Select all relevant notifications.
var notifications = ulElement.querySelectorAll('li._33c:not([data-highlighted]), li._33c[data-highlightedreact]');
for (var i=0; i < notifications.length; i++) {
var cur = notifications[i];
var displayMode = null;
if (hidden === true)
displayMode = 'none';
cur.style.display = displayMode;
}
}
// Return true if substr exists in nstr.
// substr Arrays must match every string in them.
function matchType(nstr, substr) {
switch(typeof(substr)) {
case 'string':
if (~nstr.indexOf(substr)) {
return true;
}
break;
case 'object':
var all = true;
for (var i=0; i < substr.length; i++) {
var c = substr[i];
// Every substring must exist.
if (!~nstr.indexOf(c)) {
all = false;
}
}
if (all) return true;
break;
}
}
// Return True if notification matches type.
// n - Notification text.
// t - Type String in notificationTypes.
function notificationType(n, t) {
var group = notificationTypes[t];
for (var i=0; i < group.length; i++) {
var c = group[i];
if (matchType(n, c)) {
return true;
}
}
}
// Update relevant notifications with highlight.
function updateNotifications() {
if (ulElement === null) {
ulElement = fbNotificationsFlyout.querySelector('ul');
}
// Delay until notifications elements are available.
if (ulElement === null) {
setTimeout(function(){ updateNotifications(); }, 500);
return;
}
// On first viewing.
if (first === true) {
first = false;
// Hide/Show option.
var headerActions = fbNotificationsFlyout.querySelector('div.uiHeaderActions');
var toggleOption = document.createElement("a");
toggleOption.style = "font-weight: bold;";
toggleOption.title = "Hide unimportant notifications.";
toggleOption.innerHTML = 'Hide Unimportant';
toggleOption.onclick = function(e) {
e.stopPropagation();
e.preventDefault();
if (hidden === true) {
hidden = false;
toggleOption.title = "Hide unimportant notifications.";
toggleOption.innerHTML = 'Hide Unimportant';
} else {
hidden = true;
toggleOption.title = "Show unimportant notifications.";
toggleOption.innerHTML = 'Show Unimportant';
}
updateVisibility();
};
var spacer = document.createElement("span");
spacer.setAttribute("role", "presentation");
spacer.setAttribute("aria-hidden", "true");
spacer.innerHTML = ' · ';
headerActions.insertBefore(toggleOption, headerActions.firstChild);
headerActions.insertBefore(spacer, toggleOption.nextSibling);
}
// Only check new notifications.
var notificationsNew = ulElement.querySelectorAll('li.jewelItemNew:not([data-highlighted])');
for (var i = 0; i < notificationsNew.length; i++) {
var match = false;
var current = notificationsNew[i];
// General matches by notification type.
// NOTE: Substring matches will override these in next check.
if ('dataset' in current && 'gt' in current.dataset) {
// Important notifications.
if (~current.dataset.gt.indexOf(':"from_friend"') ||
~current.dataset.gt.indexOf(':"highlights_friend_liker_commenter"') ||
~current.dataset.gt.indexOf(':"event_joined_nearby"') ||
~current.dataset.gt.indexOf(':"app_notification"') ||
~current.dataset.gt.indexOf(':"message_request"') ||
// ~current.dataset.gt.indexOf(':"watch_party_started_implicit"') ||
~current.dataset.gt.indexOf(':"appeal_content_block_message_update"') ||
~current.dataset.gt.indexOf(':"group_r2j_approved"') ||
~current.dataset.gt.indexOf(':"mention"') ||
~current.dataset.gt.indexOf(':"feed_comment_reply"')) {
match = 1;
}
// React notifications.
if (~current.dataset.gt.indexOf(':"feedback_reaction_generic"') ||
~current.dataset.gt.indexOf(':"like"')) {
match = 2;
}
// Urgent notifications.
if (~current.dataset.gt.indexOf(':"flytrap_bug_report_response"') ||
~current.dataset.gt.indexOf(':"scase_reviewed') ||
~current.dataset.gt.indexOf(':"scase_response') ||
~current.dataset.gt.indexOf(':"appeal_content_block_message_create')) {
match = 3;
}
// Mod notifications.
//if (~current.dataset.gt.indexOf(':""')) {
// match = 4;
//}
}
var notificationParent = current.querySelector('div[class="_4l_v"]');
var notificationYour = notificationParent.querySelectorAll('span:not(.fwb)');
// Loop over all new notifications.
for (var j=0; j < notificationYour.length; j++) {
// Matched before loop.
if (match) break;
var cur = notificationYour[j];
var t = cur.textContent.trim();
// Skip single word SPAN elements.
if (!~t.trim().indexOf(" ")) continue;
// Relevant text inside notification element.
if (notificationType(t, 'important')) {
match = 1;
break;
}
if (notificationType(t, 'react')) {
match = 2;
break;
}
if (notificationType(t, 'urgent')) {
match = 3;
break;
}
if (notificationType(t, 'mod')) {
match = 4;
break;
}
}
// No match, check next notification.
if (!match) continue;
// Remember highlighted element.
current.dataset.highlighted = "1";
// Select color by matching value.
var color;
switch(match) {
case 1:
color = notificationColors.darkblue;
break;
case 2:
color = notificationColors.lightpink;
current.dataset.highlightedreact = "1";
break;
case 3:
color = notificationColors.lightgreen;
break;
case 4:
color = notificationColors.darkpurple;
break;
}
// Update the li & a elements backgrounds.
current.style.backgroundColor = color;
var a = current.querySelector('a[class="_33e _1_0e"]');
if (a !== null) {
a.style.backgroundColor = color;
}
// Pass to object, for highlight removal after click.
current.paramA = a;
current.addEventListener("click", removeHighlight);
}
}
})();