forked from SaijoGeorge/HelpMeBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroost.html
116 lines (106 loc) · 5.3 KB
/
roost.html
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
<html>
<head>
<link rel="manifest" href="/roost_manifest.json">
<script>
//To customize filename, change the above and below.
var swFilename = "roost_worker.js";
var init = function() {
var hasPush = !!window.PushManager || !!navigator.push;
var hasNotification = !!window.Notification;
var hasServiceWorker = !!navigator.serviceWorker;
var supportsPush = hasPush && hasNotification && hasServiceWorker;
var logging = true;
function loadFrame(url) {
var i = document.createElement('iframe');
i.style.display = 'none';
i.src = url;
document.body.appendChild(i);
}
function parseQueryString() {
var qd = {};
location.search.substr(1).split("&").forEach(function (item) {
var parts = item.split("=");
var k = parts[0];
var v = decodeURIComponent(parts[1]);
(k in qd) ? qd[k].push(v) : qd[k] = [v, ];
});
return qd;
}
var query = parseQueryString();
if (logging) console.log(query);
if (query.noteID && query.noteID.length == 1) {
if (query.sendID && query.sendID.length == 1) {
window.location.replace("https://go.goroost.com/note/" + query.noteID[0] + "/" + query.sendID[0]);
return;
}
}
if (supportsPush) {
var alreadyAnswered = Notification.permission != "default";
var tags = query.tags;
var host = query.host && query.host.length > 0 ? query.host[0] : "https://go.goroost.com";
var alias = query.alias && query.alias.length > 0 ? query.alias[0] : null;
var appKey = query.appKey && query.appKey.length > 0 ? query.appKey[0] : null;
var firstTime = query.firstTime && query.firstTime.length > 0 ? query.firstTime[0] === "true" : !alreadyAnswered;
var params = "&appKey=" + appKey + "&firstTime=" + firstTime;
if (alias) {
params += "&alias=" + encodeURIComponent(alias);
}
if (tags) {
for (var i = 0; i < tags.length; i++) {
var tag = tags[i];
params += "&tag=" + encodeURIComponent(tag);
}
}
if (!alreadyAnswered) {
//log the prompt
loadFrame(host + "/api/frame/register?prompt=true&appKey=" + appKey);
}
//The domain that this call happens on is the one shown in the permission dialog.
Notification.requestPermission(function (permission) {
if (logging) console.log('Notification.requestPermission result.', permission);
if (permission === "granted") {
var serviceworkerURL = "/" + swFilename;
var scope = "/";
navigator.serviceWorker.register(serviceworkerURL, {scope: scope}).then(function (swr) {
if (logging) console.log("SW ready", swr);
//We don't want to fire two events in the case that the service worker does install early.
if (alreadyAnswered) {
swr.pushManager.subscribe().then(
function (pushRegistration) {
if (logging) console.log("registered", pushRegistration);
var regID = pushRegistration.subscriptionId;
//this stays the remote host, even if "custom"
loadFrame(host + "/api/frame/register?deviceToken=" + encodeURIComponent(regID) + params);
}, function (error) {
console.log(error);
}
);
}
}, function onReject(err) {
console.log("Roost Error: Could not register service worker. Validate correct file installed: " + serviceworkerURL, err);
});
if (!alreadyAnswered) {
//This is the first time, let's reload to get the worker installed
if (logging) console.log("Reloading internal frame to cement SW install.");
setTimeout(function() {
window.location.href = window.location.href + "&firstTime=" + firstTime;
}, 200);
}
}
});
}
};
if (document.readyState === 'interactive' || document.readyState === "complete") {
init();
} else {
if (document.addEventListener) {
window.addEventListener('load', init);
} else {
document.attachEvent('onload', init);
}
}
</script>
</head>
<body>
</body>
</html>