-
Notifications
You must be signed in to change notification settings - Fork 0
/
badging.js
71 lines (63 loc) · 2.07 KB
/
badging.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
// This code copied from https://badging-api.glitch.me/index.html.
// Update the UI to indicate whether the API is supported.
function isSupported(kind) {
console.log('supported', kind);
const divNotSupported = document.getElementById('notSupported');
divNotSupported.classList.toggle('hidden', true);
butSet.removeAttribute('disabled');
butClear.removeAttribute('disabled');
badgeVal.removeAttribute('disabled');
}
// Wrapper to support first and second origin trial
// See https://web.dev/badging-api/ for details.
function setBadge(...args) {
if (navigator.setExperimentalAppBadge) {
navigator.setExperimentalAppBadge(...args);
} else if (window.ExperimentalBadge) {
window.ExperimentalBadge.set(...args);
}
}
// Wrapper to support first and second origin trial
// See https://web.dev/badging-api/ for details.
function clearBadge() {
if (navigator.clearExperimentalAppBadge) {
navigator.clearExperimentalAppBadge();
} else if (window.ExperimentalBadge) {
window.ExperimentalBadge.clear();
}
}
document.addEventListener('DOMContentLoaded', ()=> {
// Check if the API is supported.
if ('setExperimentalAppBadge' in navigator) {
isSupported('v2')
}
// Check if the previous API surface is supported.
if ('ExperimentalBadge' in window) {
isSupported('v1');
}
const butSet = document.getElementById('butSet');
const butClear = document.getElementById('butClear');
const badgeVal = document.getElementById('badgeVal');
// Click event handler for Set button.
butSet.addEventListener('click', () => {
const val = parseInt(badgeVal.value, 10);
if (isNaN(val)) {
setBadge();
return;
}
setBadge(val);
});
// Click event handler for Clear button.
butClear.addEventListener('click', () => {
clearBadge();
});
// On page load, set the badge to a simple flag.
window.addEventListener('load', () => {
const val = parseInt(badgeVal.value, 10);
if (isNaN(val)) {
setBadge();
return;
}
setBadge(val);
});
})