forked from Dellos7/ms-teams-notifications-tampermonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (84 loc) · 3.19 KB
/
index.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
// ==UserScript==
// @name Microsoft Teams Notifications
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Creates browser notifications for the Web-based Teams application. Useful in Linux (in Linux notifications do not work). Tested in Chrome 69.
// @author David López Castellote
// @match https://teams.microsoft.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function notifyMe(numberOfMessages) {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notifications.");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
createNotification(numberOfMessages);
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission(function(permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
createNotification(numberOfMessages);
}
});
}
}
function createNotification(numberOfMessages) {
var title = "Microsoft Teams";
var options = {
body: "You have " + numberOfMessages + " new notifications.",
icon: document.querySelector('link[rel="icon"]').href,
requireInteraction: true
};
var notification = new Notification(title, options);
notification.onclick = function() {
window.focus();
};
}
function setTitleObserver() {
console.log('Activating Teams notifications...');
requestNotificationsPermission();
var target = document.querySelector('head > title');
var observer = new window.WebKitMutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var newTitle = mutation.target.textContent;
var res;
try {
res = newTitle.match(/(?<=\().+?(?=\))/);
}
catch( e ) {
res = newTitle.match(/\(([^)]+)\)/);
}
if (res && res[0] && res[0] && document.hidden) {
notifyMe(res[0]);
return false;
}
});
});
observer.observe(target, {
subtree: true,
characterData: true,
childList: true
});
}
function requestNotificationsPermission() {
Notification.requestPermission().then(function(result) {
console.log('Permission for Teams notifications: ' + result);
});
}
function exitFromCurrentChat() {
setInterval( function() {
if( document.hidden ) {
document.querySelector('ul[data-tid="active-chat-list"] li:nth-last-child(2) a').click();
}
}, 30000 );
}
setTitleObserver();
exitFromCurrentChat();
})();