Skip to content

Instantly share code, notes, and snippets.

@Basicprogrammer10
Last active April 29, 2021 22:39
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 Basicprogrammer10/20e681099a7f4cb6d89b8dfe13300264 to your computer and use it in GitHub Desktop.
Save Basicprogrammer10/20e681099a7f4cb6d89b8dfe13300264 to your computer and use it in GitHub Desktop.
iPhone Widget for mu WebChecklist (Requires Simple API plugin on server)
// 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