-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
64 lines (55 loc) · 1.7 KB
/
index.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
<html>
<head>
<script>
//--------- PRIVATE -----------
var api = "http://localhost:4242/api/client/features";
var cacheKey = "unleash-spa-cache";
setInterval(function () {
requestPrivate();
}, 6000);
async function requestPrivate() {
var response = await fetch(api);
if (response.status == 200) {
var text = await response.text();
localStorage.setItem(cacheKey, text);
return text;
}
}
function isEnabledPrivate(flag, cache) {
if (!cache) {
return true; //Hard decision! Returns true if it was not possible to retrieve information from cache or API.
}
var features = JSON.parse(cache).features;
return (
features.filter(function (feature) {
return (
feature.name == flag &&
feature.enabled == true &&
feature.strategies.filter(function (strategy) {
return (
strategy.name == "applicationHostname" &&
strategy.parameters.hostNames.indexOf(
window.location.hostname
) != -1
);
}).length > 0
);
}).length > 0
);
return false;
}
//--------- API -----------
async function isEnabled(flag) {
var cache = localStorage.getItem(cacheKey) || (await requestPrivate());
return isEnabledPrivate(flag, cache);
}
//--------- TEST -----------
isEnabled("webcam").then(function (enabled) {
console.log(enabled);
});
</script>
</head>
<body>
<h1>Unleash custom JS client</h1>
</body>
</html>