-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
121 lines (84 loc) · 2.73 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict'
var canUseNotifications = false;
//default value
var lastPullNewsID = -1;
//if local storage is available and not size = 0
function storageAvailable(type) {
return true;
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
console.log("wat", e);
return false;
}
}
//looking into local storage for it
function getLastPullNewsID(){
if (storageAvailable('localStorage')) {
// Yippee! We can use localStorage awesomeness
//console.log(" Yippee! We can use localStorage awesomeness!");
//if not set yet
if(!localStorage.getItem('LastPullNewsID')) {
localStorage.setItem('LastPullNewsID', -1);
}
return localStorage.getItem('LastPullNewsID');
}
else {
// Too bad, no localStorage for us
return lastPullNewsID;
}
}
function setLastPullNewsID(newID){
if (storageAvailable('localStorage')) {
// Yippee! We can use localStorage awesomeness
localStorage.setItem('LastPullNewsID', newID);
}
else {
// Too bad, no localStorage for us
lastPullNewsID = newID;
}
}
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.log("This browser does not support desktop notification");
}else if (Notification.permission === "granted") { //already granted
canUseNotifications = true;
}else if (Notification.permission !== 'denied') {
console.log("Notification ask");
// we need to ask the user for permission
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
console.log("Notification allowed");
canUseNotifications = true;
}
});
}
if (window.Worker && canUseNotifications) {
//starting worker. relative path not working...
var myWorker = new Worker("/moodle/blocks/sitdinner/notificationWorker.js?"+Math.random());
myWorker.onmessage = function(e) {
//console.log('Message received from worker:'+e.data);
var jsonData = e.data;
if(jsonData['action'] == 'openURL'){
window.location.href = "https://hembstudios.no/birdid/"+jsonData['url'];
}
if(jsonData['action'] == 'setLastPullNewsID'){
setLastPullNewsID(jsonData['newID'])
//console.log("WIndow by object: ",getLastPullNewsID());
}
if(jsonData['action'] == 'getLastPullNewsID'){
//send the stored ID
myWorker.postMessage({'action': 'setLastPullNewsID', 'newID': getLastPullNewsID()});
//console.log("WIndow by object SENDT!: ",getLastPullNewsID());
}
};
myWorker.onerror = function(e) {
console.error('Web Worker Error on line ('+e.lineno+'): '+e.message + " | In file: "+e.filename);
};
}