-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.html
55 lines (54 loc) · 1.53 KB
/
ui.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
<h3>Please Enter Your API Key</h3>
<p><input id="api-key-input" class="api-key-input" placeholder="API Key" required pattern="^(waka_)?[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-4[0-9A-Fa-f]{3}-[89ABab][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}$" /></p>
<button id="enter">Enter</button>
<script>
// Handle Input
document.getElementById('enter').onclick = () => {
const textbox = document.getElementById('api-key-input');
const apiKey = textbox.value;
if (textbox.checkValidity()) {
parent.postMessage(
{
pluginMessage: {
type: 'update-api-key',
apiKey
}
},
'*'
);
}
};
// Handle Heartbeats
window.onmessage = async (event) => {
if (event.data.pluginMessage.type === 'heartbeat') {
let data = event.data.pluginMessage.data;
await fetch('https://wakatime.com/api/v1/users/current/heartbeats', {
type: 'POST',
mode: 'no-cors',
contentType: 'application/json',
dataType: 'json',
headers: {
Authorization: `Basic ${btoa(data.apiKey)}`
},
data: {
time: data.time / 1000,
entity: data.file,
type: 'file',
project: data.project,
language: data.language,
is_write: data.isWrite ? true : false,
lines: data.lines,
plugin: `figma-wakatime/${data.VERSION}`
}
});
}
};
</script>
<style>
.api-key-input:invalid {
border: red solid 3px;
}
.api-key-input:valid {
border: green solid 3px;
}
</style>