Skip to content

Instantly share code, notes, and snippets.

@nyuszika7h

nyuszika7h/Namedays.js

Last active Sep 28, 2020
Embed
What would you like to do?
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