Skip to content

Instantly share code, notes, and snippets.

@0xallie
Last active April 27, 2024 04:46
Show Gist options
  • Save 0xallie/908f83f2557578c4072a4acae716454e to your computer and use it in GitHub Desktop.
Save 0xallie/908f83f2557578c4072a4acae716454e to your computer and use it in GitHub Desktop.
iOS 14 Scriptable Namedays widget
async function getCountry() {
let r = new Request('https://ipinfo.io/country')
let s = await r.loadString()
return s.trim()
}
function pad(n) {
return n.toString().padStart(2, '0')
}
let country = args.widgetParameter || await getCountry()
let d = new Date()
let month = pad(d.getMonth() + 1)
let day = pad(d.getDate())
let title = `Namedays (${month}.${day})`
let namedays = []
let r = new Request(`https://api.abalin.net/namedays?country=${country}&month=${month}&day=${day}`)
let j = await r.loadJSON()
if (j.status_code >= 400) {
title = 'Unsupported country'
namedays = ['Set country in widget settings\n(see https://api.abalin.net/)']
} else {
namedays = j.data.namedays[country.toLowerCase()]
namedays = namedays.split(',')
}
function createWidget() {
let w = new ListWidget()
w.addSpacer()
let t = w.addText(title)
t.applyHeadlineTextStyling()
t.centerAlignText()
w.addSpacer(10)
let b = w.addText(namedays.join(', '))
b.centerAlignText()
w.addSpacer()
return w
}
if (config.runsInWidget) {
let w = createWidget()
Script.setWidget(w)
} else {
let a = new Alert()
a.title = title
a.message = namedays.join(', ')
a.presentAlert()
}
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment