Forked from mattapperson/Scriptable DailyText widget.js
Last active
October 2, 2021 12:31
-
-
Save BoxOfSnoo/65eb37b493164e573ce5db11aee6da03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dailyText = await loadText() | |
if (config.runsInWidget) { | |
let widget = createWidget(dailyText) | |
Script.setWidget(widget) | |
Script.complete() | |
} else { | |
Safari.open(dailyText.wtlibUrl); | |
} | |
function createWidget(dailyText) { | |
const scripture = extractScripture(dailyText).replace(/<[^>]*>?/gm, ''); | |
const text = extractText(dailyText).replace(/<[^>]*>?/gm, '') | |
let w = new ListWidget() | |
w.backgroundColor = new Color("#4a6da7") | |
let titleTxt = w.addText(scripture) | |
titleTxt.textColor = Color.white() | |
let article; | |
switch (config.widgetFamily) { | |
case "extraLarge": | |
titleTxt.font = new Font("San Francisco Display Bold", 20); | |
article = w.addText(text); | |
article.textColor = Color.white(); | |
article.textOpacity = 0.8; | |
article.font = new Font("San Francisco Display", 17); | |
article.lineLimit = 20; | |
break; | |
case "large": | |
titleTxt.font = new Font("San Francisco Display Bold", 17); | |
article = w.addText(text); | |
article.textColor = Color.white(); | |
article.textOpacity = 0.8; | |
article.font = new Font("San Francisco Display", 15); | |
article.lineLimit = 15; | |
break; | |
case "medium": | |
titleTxt.font = new Font("San Francisco Display Bold", 15); | |
article = w.addText(text); | |
article.textColor = Color.white(); | |
article.textOpacity = 0.8; | |
article.font = new Font("San Francisco Display", 15); | |
article.lineLimit = 5; | |
break; | |
case "small": | |
titleTxt.font = new Font("San Francisco Display Bold", 15); | |
titleTxt.centerAlignText(); | |
break; | |
} | |
return w | |
} | |
async function loadText() { | |
const date = new Date() | |
let url = `https://wol.jw.org/wol/dt/r1/lp-e/${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}` | |
let req = new Request(url) | |
let json = await req.loadJSON() | |
let docLink = extractDocLink(json.items[0]) | |
let wtlibUrl = `https://www.jw.org/finder?wtlocale=E&prefer=lang&docid=${docLink}` | |
// Inject the url into the object | |
json.items[0]['wtlibUrl'] = wtlibUrl | |
return json.items[0] | |
} | |
function extractScripture(item) { | |
let regex = /<em>(.*)<\/em>/ | |
let html = item.content | |
let matches = html.match(regex) | |
if (matches && matches.length >= 2) { | |
return matches[1] | |
} else { | |
return null | |
} | |
} | |
function extractText(item) { | |
let html = item.content.split("<p")[2].split(/>(.+)/)[1] | |
if (html) { | |
return html | |
} else { | |
return null | |
} | |
} | |
function extractDocLink(item) { | |
let paragraph = item.content.match(/data-pid="(\d*?)"/)[1]; | |
if (paragraph) { | |
let range = paragraph+"-"+(+paragraph+2) | |
return item.did +"&par="+range | |
} else { | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment