Skip to content

Instantly share code, notes, and snippets.

@OriginalJef
Last active October 9, 2020 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OriginalJef/c8568647457703cf778ab73ecdf93342 to your computer and use it in GitHub Desktop.
Save OriginalJef/c8568647457703cf778ab73ecdf93342 to your computer and use it in GitHub Desktop.
Scriptable widget for random dad Jokes
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
const url = `https://icanhazdadjoke.com/slack`
const req = new Request(url)
const res = await req.loadJSON()
if (config.runsInWidget) {
// create and show widget
let widget = createWidget("Dad Joke", `${res.attachmenets[0}.fallback`, "#35363A")
Script.setWidget(widget)
Script.complete()
} else {
// make table
let table = new UITable()
// add header
let row = new UITableRow()
row.isHeader = true
row.addText(`Dad Joke`)
table.addRow(row)
// fill data
table.addRow(createRow("Cases", res.attachmenets[0}.fallback))
if (config.runsWithSiri)
Speech.speak(res.attachmenets[0}.fallback)
// present table
table.present()
}
function createRow(title) {
let row = new UITableRow()
row.addText(title)
return row
}
function createWidget(pretitle, subtitle, color) {
let w = new ListWidget()
w.backgroundColor = new Color(color)
let preTxt = w.addText(pretitle)
preTxt.textColor = Color.white()
preTxt.textOpacity = 0.8
preTxt.font = Font.systemFont(16)
w.addSpacer(5)
let subTxt = w.addText(subtitle)
subTxt.textColor = Color.white()
subTxt.textOpacity = 0.8
subTxt.font = Font.systemFont(12)
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment