-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathspecial-days-widget.js
More file actions
123 lines (100 loc) · 3.85 KB
/
Copy pathspecial-days-widget.js
File metadata and controls
123 lines (100 loc) · 3.85 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
SPECIAL DAYS (small widget)
Setup
- Variables with the suffix `ColourLight` or `ColourDark`should be a hex colour string. The colours are used as the light mode or dark mode colour for what the variable name describes
- Variables with the suffix `Size` should be a number for the text size described by the variable name
- `showTitle` should be a boolean to show the title or not
*/
let backgroundColourLight = "#fff"
let backgroundColourDark = "#000"
let titleSize = 15
let titleColourLight = "#000"
let titleColourDark = "#fff"
let showTitle = true
let daySize = 15
let dayColourLight = "#000"
let dayColourDark = "#fff"
/*
DO NOT EDIT THE REST OF THE CODE
*/
// Sets correct colours
const backgroundColour = Color.dynamic(new Color(backgroundColourLight),new Color(backgroundColourDark))
const titleColour = Color.dynamic(new Color(titleColourLight),new Color(titleColourDark))
const dayColour = Color.dynamic(new Color(dayColourLight),new Color(dayColourDark))
// Makes widget
let widget = new ListWidget()
widget.backgroundColor = backgroundColour
let date = new Date()
date.setHours(date.getHours() + 1)
widget.refreshAfterDate = date
widget.url = "https://www.daysoftheyear.com/"
// Adds the title to the widget
if (showTitle) {
let title = widget.addText("Today Is...")
title.font = Font.regularSystemFont(titleSize)
title.textColor = titleColour
title.centerAlignText()
widget.addSpacer(5)
}
// Gets the special days data
const data = await getData()
// Adds the days to the widget
let days = widget.addText(parseHtmlEntities(data.dayArray.join("\n")))
days.font = Font.regularSystemFont(daySize)
days.textColor = dayColour
days.minimumScaleFactor = .5
// Compleats the script
widget.presentSmall()
Script.setWidget(widget)
Script.complete()
// Function to replace common html entities
function parseHtmlEntities(str) {
return str.replace(/&#([0-9]{1,4});/g, function(match, numStr) {
var num = parseInt(numStr, 10)
return String.fromCharCode(num)
})
.replace(/ /g, " ")
.replace(/&/g, "&")
.replace(/'/g, "'")
}
// Function to get and save the special day data for the day
async function getData() {
// Set up variables for working with the file
const fm = FileManager.iCloud()
const baseDir = fm.documentsDirectory()
let file = fm.joinPath(baseDir, "specialDayData.json")
// Find the current date and last updated date of the file
let currentDate = new Date()
let updated = fm.modificationDate(file) || new Date()
// See if the file does not exist or was not yet modified today
if(!fm.fileExists(file) || currentDate.toDateString() != updated.toDateString()) {
// If it was, get the data
const req = new Request("https://www.daysoftheyear.com/")
const html = await req.loadString()
// Get the current daye and set up a variable to store the days
let currentDate = new Date()
currentDate = currentDate.toDateString()
const dayArray = []
// Get up to 5 special days
for(let i = 1; i < 6; i++) {
// Match out the date
let date = new Date(html.match(/<div class="date_day">(.+?)<\/div>/g)[i].match(/<div class="date_day">(.+?)<\/div>/)[1].match(/<time.+?="(.+?)">/)[1])
// Convert date to the same format as the current date
date = date.toDateString()
// Match out the special day
let day = html.match(/class="js-link-target">.+?<\/a>/g)[i].match(/class="js-link-target">(.+?)<\/a>/)[1].replace(/<[^>]*>/g, "")
// If the current date is the special day's date, add it to the array
if (date === currentDate) {
dayArray.push(day)
}
}
// Write the data
fm.writeString(file, JSON.stringify({dayArray}))
}
// Read and return the file
if (!fm.isFileDownloaded(file)) {
await fm.downloadFileFromiCloud(file)
}
file = JSON.parse(fm.readString(file))
return file
}