-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (58 loc) · 2.03 KB
/
index.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
const URL = "https://static.easysunday.com/covid-19/getTodayCases.json"
async function getCovidData() {
// a function that get requests from Sunday Technologies
const request = new Request(URL)
return await request.loadJSON()
}
function parseDate(data) {
// format date to system string
return new Date(data.updated).toLocaleDateString()
}
function addDateText(stack, text) {
// add date string to widget
let dateText = stack.addText(text + "\n")
dateText.centerAlignText()
dateText.font = Font.semiboldSystemFont(13)
dateText.textColor = new Color("#ffffff")
}
function addTodayCasesText(stack, text) {
// add cases string to widget
let casesText = stack.addText(text);
casesText.centerAlignText()
casesText.font = Font.boldSystemFont(30)
casesText.textColor = new Color("#ffffff")
}
function addCreditText(stack) {
// add credit string to widget
let creditText = stack.addText("ข้อมูลจาก Sunday Technologies")
creditText.centerAlignText()
creditText.font = Font.semiboldSystemFont(6)
creditText.textColor = new Color("#ffffff")
}
function displayData(stack, updatedDate, data) {
// a function that display all data in widget
addTodayCasesText(stack, data.todayCases.toLocaleString())
addDateText(stack, updatedDate)
addCreditText(stack)
}
async function createWidget() {
// a function that create widget
let listWidget = new ListWidget()
listWidget.backgroundColor = new Color("#f95e5e")
let heading = listWidget.addText("วันนี้ติดกันกี่คน ?")
heading.centerAlignText()
heading.font = Font.regularSystemFont(14)
heading.textColor = new Color("#ffffff")
listWidget.addSpacer(5)
let data = await getCovidData()
let updatedDate = parseDate(data)
displayData(listWidget, updatedDate, data)
return listWidget
}
let widget = await createWidget()
if (config.runsInWidget) {
Script.setWidget(widget)
} else {
widget.presentSmall()
}
Script.complete()