Last active
September 25, 2020 00:54
-
-
Save SaoYan/fbc84eab6fac00a8562cd2cc0d7b3b27 to your computer and use it in GitHub Desktop.
iOS Scriptable script - customizing widget
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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: orange; icon-glyph: quote-right; | |
// Thanks to: https://gist.github.com/spencerwooo/7955aefc4ffa5bc8ae7c83d85d05e7a4 | |
/** | |
* query keys for each site | |
*/ | |
const github = "SaoYan" | |
const zhihu = "harry-yiqi-yan" | |
/** | |
* display color for each site | |
*/ | |
const dispColor = { | |
Github: "#ff9468", | |
Zhihu: "#2e96ff" | |
} | |
/** | |
* gather follower info for each site | |
*/ | |
let siteStats = {} | |
const githubStats = await fetchSiteStats(`https://api.github.com/users/${github}`) | |
siteStats["Github"] = githubStats === null ? -1 : githubStats.followers | |
const zhihuStats = await fetchSiteStats(`https://www.zhihu.com/api/v4/members/${zhihu}?include=follower_count`) | |
siteStats["Zhihu"] = zhihuStats === null ? -1 : zhihuStats.follower_count | |
/** | |
* create widget | |
*/ | |
const widget = createWidget(siteStats) | |
Script.setWidget(widget) | |
Script.complete() | |
async function fetchSiteStats(url) { | |
const request = new Request(url) | |
const res = await request.loadJSON() | |
if (request.response.statusCode !== 200) { | |
return null; | |
} else { | |
return res; | |
} | |
} | |
function createWidget(data) { | |
console.log(data) | |
const w = new ListWidget() | |
const bgColor = new LinearGradient() | |
bgColor.colors = [new Color("#1c1c1c"), new Color("#29323c")] | |
bgColor.locations = [0.0, 1.0] | |
w.backgroundGradient = bgColor | |
w.spacing = 2 | |
const deviceLine = w.addText(`📱 ${Device.name()}`) | |
deviceLine.textSize = 5 | |
deviceLine.textColor = Color.white() | |
deviceLine.textOpacity = 0.7 | |
deviceLine.leftAlignText() | |
const osLine = w.addText(`⚙️ ${Device.systemName()} ${Device.systemVersion()}`) | |
osLine.textSize = 5 | |
osLine.textColor = Color.white() | |
osLine.textOpacity = 0.7 | |
osLine.leftAlignText() | |
const batteryLine = w.addText(`🔋 ${Math.round(Device.batteryLevel() * 100)}%`) | |
batteryLine.textSize = 5 | |
batteryLine.textColor = new Color("#6ef2ae") | |
batteryLine.leftAlignText() | |
let siteLine | |
for (let site in siteStats) { | |
const stats = siteStats[site]; | |
if (stats >= 0) { | |
siteLine = w.addText(`${site}: ${stats}`) | |
siteLine.textSize = 5 | |
siteLine.textColor = new Color(dispColor[site]) | |
siteLine.leftAlignText() | |
} | |
} | |
return w | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment