Skip to content

Instantly share code, notes, and snippets.

@hutt
Last active September 20, 2024 21:23
Show Gist options
  • Select an option

  • Save hutt/9b89df25605f6978e01b791a8dd1a0f8 to your computer and use it in GitHub Desktop.

Select an option

Save hutt/9b89df25605f6978e01b791a8dd1a0f8 to your computer and use it in GitHub Desktop.
Scriptable widget that fetches your current tracking status from Kimai and displays it on your iOS homescreen.
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: stopwatch;
// Configuration
const kimaiUrl = "https://kimai.yourdomain.com";
const apiKey = "<your api key>";
// Create widget
const widget = await createWidget();
if (!config.runsInWidget) {
await widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function createWidget() {
const widget = new ListWidget();
// Add heading
const title = widget.addText("Kimai");
title.font = Font.boldSystemFont(20);
// Fetch current time entries
const isTracking = await fetchTrackingStatus();
// Text element for status
const message = widget.addText(isTracking ? "▶︎ tracking active" : "■ tracking inactive");
message.font = Font.systemFont(13);
// Add a refresh timer
widget.refreshAfterDate = new Date(Date.now() + 1000 * 60 * 15); // 15 minutes
return widget;
}
async function fetchTrackingStatus() {
const url = `${kimaiUrl}/api/timesheets/active`;
const request = new Request(url);
request.headers = {
"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"
};
request.method = "GET";
try {
const response = await request.loadJSON();
return response && response.length > 0;
} catch (error) {
console.error(`Couldn't fetch tracking status: ${error}`);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment