-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcounter.js
55 lines (46 loc) · 1.35 KB
/
counter.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
// Returns the count for the 'Today' list (defaults to 0)
function currentCount() {
return document.querySelector('#top_filters > li:nth-child(2) > span.item_content > small').innerText || 0;
}
// Returns the title without any annotated count
function titleWithoutCount() {
const title = document.title;
const indexOfCount = title.indexOf(" (");
if (indexOfCount >= 0) {
return title.substring(0, indexOfCount);
} else {
return title;
}
}
// Returns the existing count from the title (defaults to 0)
function existingCount() {
const title = document.title;
const indexOfCount = title.indexOf(" (");
return title.substring(indexOfCount + 2, title.length - 1) || 0
}
// Update badge count based on the number in Today
function updateBadgeCount() {
const count = currentCount();
const title = titleWithoutCount();
var newTitle = document.title;
if (count === 0) {
newTitle = title;
} else if (count !== existingCount()) {
newTitle = `${title} (${count})`;
}
if (document.title !== newTitle) {
document.title = newTitle
}
}
// Update the badge every 5 seconds
setInterval(updateBadgeCount, 5000);
// Update the badge when the title changes
new MutationObserver(function(mutations) {
updateBadgeCount();
}).observe(
document.querySelector('title'), {
subtree: true,
characterData: true,
childList: true
}
);