-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunread.js
108 lines (77 loc) · 3 KB
/
unread.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
var lastnotificationcount = 0;
function refreshUnreadTotalExtension()
{
var totalunread = 0;
//console.log("refreshUnreadTotalExtension is running");
if (document.querySelector('.conversation-unread') !== null) {
var x = document.getElementsByClassName("conversation-unread");
var i;
for (i = 0; i < x.length; i++) {
var unreadinthisconvo = Number(x[i].innerHTML);
//console.log('unread in convo nr ' + i + ' is ' + unreadinthisconvo);
totalunread += unreadinthisconvo;
//console.log(' total unread is ' + totalunread);
}
document.title = 'Siilo (' + totalunread + ')';
var unreadconvofirst = document.getElementsByClassName("conversation-main-name")[0].innerHTML;
var unreadconvofirsttext = document.getElementsByClassName("conversation-message-text")[0].innerHTML.replace(/(<([^>]+)>)/ig,"").replace( /\s\s+/g, ' ' );
var zin = 'Siilo ('+ totalunread + '): ' + unreadconvofirst + ':\r\n' + unreadconvofirsttext;
console.log(zin);
if(totalunread > lastnotificationcount && !document.hasFocus())
{
let myNotification = new Notification('Siilo', {
body: zin
});
}
lastnotificationcount = totalunread;
} else {
//console.log(' total unread: none');
document.title = 'Siilo';
/*
if(process.platform === "win32")
{
mainWindow.setOverlayIcon(null,null);
}
*/
}
}
//refreshUnreadTotalExtension();
//setInterval(refreshUnreadTotalExtension, 1000);
function addObserverIfDesiredNodeAvailable() {
var composeBox = document.querySelector(".conversations");
if(!composeBox) {
//The node we need does not exist yet.
//Wait 500ms and try again
//alert(".conversations not available");
window.setTimeout(addObserverIfDesiredNodeAvailable,500);
return;
}
//alert(".conversations is available");
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
//alert("added MutationObserver");
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
//alert("added EventListener");
}
};
})();
// Observe a specific DOM element:
observeDOM( document.querySelector('.conversations') ,function(){
//alert('dom changed');
refreshUnreadTotalExtension();
});
}
addObserverIfDesiredNodeAvailable();