-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamedde_Minecraft_Server_Console_User_Notification.user.js
More file actions
83 lines (71 loc) · 2.74 KB
/
Copy pathgamedde_Minecraft_Server_Console_User_Notification.user.js
File metadata and controls
83 lines (71 loc) · 2.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// ==UserScript==
// @name Minecraft User Joined Notification
// @namespace http://romibi.ch/
// @downloadURL https://raw.githubusercontent.com/romibi/MyUserscripts/master/gamedde_Minecraft_Server_Console_User_Notification.user.js
// @version 0.2
// @description Get Notified when a new user joined!
// @author romibi
// @match https://wsproxy.united-gameserver.de:8080/console/*/
// @icon https://www.google.com/s2/favicons?domain=united-gameserver.de
// @grant none
// ==/UserScript==
(function(){
var canNotify = false;
var enc = new TextDecoder("utf-8");
var askNotificationPermission = function() {
if(canNotify) return;
//console.log(Notification.permission);
if (Notification.permission === "granted") {
canNotify = true;
} else if (Notification.permission !== "denied") {
Notification.requestPermission().then(permission => {
//console.log(permission);
});
}
}
if (Notification.permission === "granted") {
canNotify = true;
//console.log(Notification.permission);
} else {
document.body.onclick = askNotificationPermission;
}
var showNotify = function(message) {
//console.log("showing notification: "+message);
const notification = new Notification("Minecraft Server Notification", {body: message, icon: 'https://romibi.ch/images/bluehat-gaming.png'});
notification.onclick = function() { window.focus(); this.cancel(); }
}
var notify = function(message) {
if(canNotify) {
showNotify(message);
}
}
var checkMessageForNotification = function(message) {
var match = message.match(/: (\w*)\[.*?\] logged in/);
//console.log("no match with: "+message);
if(match !== null) {
//console.log(match[1] + " logged in");
notify(match[1] + " logged in");
}
}
var logMessage = function(event){
//console.log(event);
var message = enc.decode(event.data);
var lines = message.split("\n");
for ( const line of lines) {
var cleaned = line.replace(/\u001b\[\d{0,2}m/g,"")
checkMessageForNotification(cleaned);
//console.log(cleaned);
}
}
var ws = window.WebSocket;
window.WebSocket = function (a, b) {
var that = b ? new ws(a, b) : new ws(a);
//that.addEventListener("open", console.info.bind(console, "socket open"));
//that.addEventListener("close", console.info.bind(console, "socket close"));
//that.addEventListener("message", console.info.bind(console, "socket msg"));
that.addEventListener("message", logMessage);
return that;
};
window.WebSocket.prototype=ws.prototype;
window.WebSocket.OPEN = 1; // Apparently gamed (or a used lib) defined some custom constants …
}());