-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnotifyFriends.local.js
More file actions
60 lines (58 loc) · 1.74 KB
/
notifyFriends.local.js
File metadata and controls
60 lines (58 loc) · 1.74 KB
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
// Offline never happens, you have to navigate to the friends page before anyone goes "offline" (this caching is weird)
eventManager.on('ChatDetected', () => {
/*
let socket;
function openSocket() {
socket = new WebSocket(`wss://${location.hostname}/chat`);
socket.onopen = onOpen;
socket.onmessage = onMessage;
}
function onOpen() {
socket.send(JSON.stringify({action: 'getOnlineFriends'}));
}
function onMessage(event) {
const data = JSON.parse(event.data);
let friends;
if (data.action === 'getSelfInfos' || data.action === 'getOnlineFriends') {
friends = JSON.parse(data.friends);
} else {
return;
}
console.log(data.action, friends);
if (data.action !== 'getOnlineFriends') return;
socket.close();
socket = null;
}
$(window).on('beforeunload', () => {
if (socket) socket.close();
});
*/
let updatingFriends = false;
function checkFriends(delay = 10000) {
setTimeout(() => {
//openSocket();
if (socketChat.readyState !== 1) return;
updatingFriends = true;
socketChat.send(JSON.stringify({action: 'getOnlineFriends'}));
}, delay);
}
eventManager.on('preChat:getOnlineFriends', function (data) {
if (!updatingFriends) return;
updatingFriends = false;
this.canceled = true;
const friends = {};
JSON.parse(data.friends).forEach((friend) => {
friends[friend.id] = friend;
});
selfFriends.forEach((friend) => {
// id, online, idGame, username
const now = friends[friend.id];
if (friend.online !== now.online) {
friend.online = now.online;
fn.toast(`${friend.username} is now ${now.online ? 'online' : 'offline'}.`);
}
});
checkFriends();
});
checkFriends();
});