Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save drunkensouljah/799513dd51fcf760c815104d57b3790c to your computer and use it in GitHub Desktop.

Select an option

Save drunkensouljah/799513dd51fcf760c815104d57b3790c to your computer and use it in GitHub Desktop.
WordPress plugin stat widget for iOS 14 and Scriptable app
/**
* Setup Begin
*/
const pluginSlug = "hey-notify";
const backgroundColor = "#009bff"; // In HEX format
const textColor = "#ffffff"; // In HEX format
/**
* Setup End
*/
const apiUrl = `https://api.wordpress.org/plugins/info/1.0/${pluginSlug}.json`;
const request = new Request(apiUrl);
const json = await request.loadString();
const data = JSON.parse(json);
if (config.runsInWidget) {
let widget = createWidget(data);
Script.setWidget(widget);
Script.complete();
} else {
let widget = createWidget(data);
await widget.presentSmall(); // Preview small widget
// await widget.presentMedium(); // Preview medium widget
// await widget.presentLarge(); // Preview large widget
}
function createWidget(data) {
const bgColor1 = new Color(backgroundColor);
const bgColor2 = new Color(lightenDarkenColor(backgroundColor, -25));
const gradientBackground = new LinearGradient();
gradientBackground.colors = [bgColor1, bgColor2];
gradientBackground.locations = [0,1];
const widget = new ListWidget();
widget.backgroundGradient = gradientBackground;
widget.url = `https://wordpress.org/plugins/${pluginSlug}/`;
const title = widget.addText(data.name);
title.textColor = new Color(textColor);
title.font = Font.boldSystemFont(14);
title.leftAlignText();
const downloads = widget.addText(data.downloaded.toLocaleString());
downloads.textColor = new Color(textColor);
downloads.font = Font.lightSystemFont(getFontSize(data.downloaded));
downloads.leftAlignText();
const downloadsTitle = widget.addText("DOWNLOADS");
downloadsTitle.textColor = new Color(textColor);
downloadsTitle.textOpacity = 0.5;
downloadsTitle.font = Font.heavySystemFont(10);
downloadsTitle.leftAlignText();
const openSupportTopics = data.support_threads - data.support_threads_resolved;
const supportThreads = widget.addText(openSupportTopics.toLocaleString());
supportThreads.textColor = new Color(textColor);
supportThreads.font = Font.lightSystemFont(28);
supportThreads.leftAlignText();
const supportTitle = widget.addText("SUPPORT TOPICS");
supportTitle.textColor = new Color(textColor);
supportTitle.textOpacity = 0.5;
supportTitle.font = Font.heavySystemFont(10);
supportTitle.leftAlignText();
return widget;
}
function getFontSize(number) {
if (number > 99999999) {
return 20;
}
if (number > 9999999) {
return 22;
}
if (number > 999999) {
return 26;
}
if (number > 99999) {
return 30;
}
return 38;
}
function lightenDarkenColor(color, amount) {
let usePound = false;
if (color[0] == "#") {
color = color.slice(1);
usePound = true;
}
let number = parseInt(color, 16);
let r = (number >> 16) + amount;
if (r > 255) {
r = 255;
} else if (r < 0) {
r = 0;
}
let b = ((number >> 8) & 0x00FF) + amount;
if (b > 255) {
b = 255;
} else if (b < 0) {
b = 0;
}
let g = (number & 0x0000FF) + amount;
if (g > 255) {
g = 255;
} else if (g < 0) {
g = 0;
}
[r, g, b] = [r,g,b].map(color => color <= 15 ? `0${color.toString(16)}` : color.toString(16));
return (usePound ? "#" : "") + r + b + g;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment