-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.js
86 lines (68 loc) · 2.69 KB
/
buttons.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
let widgetclickCount = 0;
const postToSW = async (action, input, payload) => {
const sw = await navigator.serviceWorker.getRegistration();
sw.active.postMessage({
action,
input,
payload,
});
};
const getByTag = async () => {
console.log('Pushing getByTag request to SW');
const tag = document.getElementById('getByTagInput').value;
postToSW('getByTag', tag);
};
const getByInstanceId = async () => {
console.log('Pushing getByInstanceId request to SW');
const instanceId = document.getElementById('getByInstanceIdInput').value;
postToSW('getByInstanceId', instanceId);
};
const getByHostId = async () => {
console.log('Pushing getByHostId request to SW');
const hostId = document.getElementById('getByHostIdInput').value;
postToSW('getByHostId', hostId);
};
const matchAll = async () => {
console.log('Pushing matchAll request to SW');
const matchAllOptions = document.getElementById('matchAllInput').value;
postToSW('matchAll', matchAllOptions);
};
const updateByTag = async () => {
console.log('Pushing updateByTag request to SW');
const tag = document.getElementById('updateByTagInput').value;
const payload = document.getElementById('updateByTagPayloadInput').value;
postToSW('updateByTag', tag, payload);
};
const updateByInstanceId = async () => {
console.log('Pushing updateByInstanceId request to SW');
const instanceId = document.getElementById('updateByInstanceIdInput').value;
const payload = document.getElementById('updateByInstanceIdPayloadInput').value;
postToSW('updateByInstanceId', instanceId, payload);
};
const onInputKeydown = (event, action) => {
if (event.key == 'Enter')
action(event.target.value);
}
const incrementWidgetClick = () => {
widgetclickCount++;
document.getElementById('widgetclickCount').textContent = widgetclickCount;
}
document.getElementById('getByTagInput').addEventListener('keydown', (event) => {
onInputKeydown(event, getByTag)});
document.getElementById('getByInstanceIdInput').addEventListener('keydown', (event) => {
onInputKeydown(event, getByInstanceId)});
document.getElementById('updateByTagInput').addEventListener('keydown', (event) => {
onInputKeydown(event, updateByTag)});
document.getElementById('updateByInstanceIdInput').addEventListener('keydown', (event) => {
onInputKeydown(event, updateByInstanceId)});
navigator.serviceWorker.addEventListener('message', (event) => {
switch(event.data.type) {
case 'showResult':
document.getElementById('resultAction').textContent = event.data.action;
document.getElementById('resultAdditionalText').textContent = ` ${event.data.additionalText}`;
break;
case 'widgetclick':
incrementWidgetClick();
break;
}
});