Skip to content

Instantly share code, notes, and snippets.

@danielkhoo
Created September 3, 2022 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkhoo/8c27b141c247221b99e32c687adc877d to your computer and use it in GitHub Desktop.
Save danielkhoo/8c27b141c247221b99e32c687adc877d to your computer and use it in GitHub Desktop.
Scriptable: npm download widget
const PACKAGE = "nric"
let widget = await createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Runs inside a widget so add it to the homescreen widget
Script.setWidget(widget);
} else {
// Show the medium widget inside the app
widget.presentSmall();
}
Script.complete();
async function createWidget() {
// Create new empty ListWidget instance
let listwidget = new ListWidget();
// Set new background color
listwidget.backgroundColor = new Color("#212121");
let response = await fetchData();
displayData(listwidget, response);
// Return the created widget
return listwidget;
}
async function fetchData() {
// Query url
const url = `https://api.npmjs.org/downloads/point/last-week/${PACKAGE}`;
// Initialize new request
const request = new Request(url);
// Execute the request and parse the response as json
const response = await request.loadJSON();
// Return the returned launch data
return response;
}
function displayData(stack, response) {
const { downloads, package } = response
// Heading
let heading = stack.addText(package);
heading.centerAlignText();
heading.font = Font.lightSystemFont(28);
heading.textColor = new Color("#ffffff");
stack.addSpacer(15);
let subheading = stack.addText("weekly downloads");
subheading.centerAlignText();
subheading.font = Font.lightSystemFont(16);
subheading.textColor = new Color("#ffffff");
stack.addSpacer(15);
// Display values
let display = stack.addText(`${downloads}`);
display.centerAlignText();
display.font = Font.lightSystemFont(28);
display.textColor = new Color("#ffffff");
const num = 10
num.toString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment