summaryrefslogtreecommitdiff
path: root/index.html
blob: 723e46e2be971844de6edb26e88d5d07489b6734 (plain)
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
<meta charset="UTF-8">
<body onload="init()">
<!--- Import jquery-->
<script src="jquery-3.3.1.min.js"></script>
<script>
//Global unix timestamp of the last time a message was displayed, any messages
//from the backend after this time are displayed and the variable is updated to
//that timestamp. It is initialized as the current time.
lastMessageTime = Date.now();
//function called after permission has been requested
function handlePermissions(result) {
	console.log("Permission "+ result);
	permissionDisplay = document.getElementsByClassName("pd")[0];
	grantedMsg = "You have granted notification permissions";
	deniedMsg = "You have not  granted notification permissions";
	//updates permissions
	if(result == "granted"){
		permissionDisplay.innerHTML = grantedMsg;
	} else {
		permissionDisplay.innerHTML = deniedMsg;
	}
	return;
}
//gets notification permissions
function getNotPerm() {
	//makes handlePermissions() the callback
	Notification.requestPermission().then(handlePermissions);
	//requests permissions
	Notification.requestPermission();
	return;	
}
//sets up the query timer and gets permissions
function init() {
	getNotPerm();
	queryTimer = setInterval(queryBackend, 1000);
}
//sends a new message
function notify() {
	msg = new Notification("あなたはチンチンが大好き");
}
//performing an https POST on the backend
function queryBackend() {
	$.post("https://" + window.location.hostname + ":6969/api/msg",
		{ld : 0},// data to submit
		function(data, status, jqXHR) { //callback function
			if (data == ""){
				return;
			}
			console.log(data);
			timestamp = parseInt(data);
			if (timestamp != NaN) {
				if (timestamp > lastMessageTime){
					notify();
					lastMessageTime = timestamp;
				}
			}
		}
	);
}
</script>
<!--- Permission display-->
<p class="pd">You have not granted notification permissions</p>
<button onclick="getNotPerm()">Re-request Notification Permissions</button>
<button onclick="notify()">Send Notifications</button>
</body>