Last active
April 29, 2021 22:39
-
-
Save Basicprogrammer10/20e681099a7f4cb6d89b8dfe13300264 to your computer and use it in GitHub Desktop.
iPhone Widget for mu WebChecklist (Requires Simple API plugin on server)
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
// Check List | |
// Display WebChecklist items through a widget | |
// Requires Simple API plugin (v1.0+) | |
let server = ""; // Server Adress (Not Api Path) | |
let list = ""; // Checklist Name | |
let overflowIndex = 4; | |
// Get Request | |
async function getRequest(url) { | |
let req = new Request(url); | |
let json = await req.loadJSON(); | |
return json; | |
} | |
// Gen random hex color | |
function randomColor() { | |
let color = "#"; | |
for (let i = 0; i < 6; i++) { | |
const random = Math.random(); | |
const bit = (random * 16) | 0; | |
color += bit.toString(16); | |
} | |
return color; | |
} | |
// Filter Api Data | |
function getUnChecked(data) { | |
let working = data.filter((item) => !item.checked); | |
return working; | |
} | |
// Add text to a widget | |
function addWidgetText(widget, str, color, font) { | |
let text = widget.addText(str); | |
text.textColor = new Color(color); | |
text.font = Font.boldSystemFont(font); | |
widget.addSpacer(6); | |
} | |
// Main Function | |
async function main(server, list) { | |
let jsonResponse = await getRequest(server + "/simple/" + list); | |
let todo = getUnChecked(jsonResponse); | |
let overFlow = false; | |
todo.forEach((item, index) => { | |
if (index > overflowIndex) { | |
overFlow = true; | |
return; | |
} | |
addWidgetText(widget, item.name, randomColor(), 13); | |
}); | |
if (overFlow) addWidgetText(widget, `And ${todo.length - overflowIndex - 1} more...`, randomColor(), 15); | |
} | |
// Run Main Function and Exit Script | |
let widget = new ListWidget(); | |
await main(server, list); | |
Script.setWidget(widget); | |
widget.presentSmall(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment