自己動(dòng)手做個(gè)iOS14小組件顯示嗶哩嗶哩UP主粉絲數(shù)
如果你是一個(gè)嗶哩嗶哩UP主,是否時(shí)時(shí)刻刻想知道自己有多少個(gè)粉絲呢?那就做一個(gè)吧。
需要用到“Scriptable”這個(gè)APP,免費(fèi)無(wú)內(nèi)購(gòu),請(qǐng)搜索安裝。
https://apps.apple.com/cn/app/scriptable/id1405459188
使用bilibili的API,http://api.bilibili.com/x/relation/stat?vmid=280793434
這里使用手工耿的ID測(cè)試,返回json格式結(jié)果:
{"code":0,"message":"0","ttl":1,"data":{"mid":280793434,"following":32,"whisper":0,"black":0,"follower":4573391}}
這是站長(zhǎng)寫(xiě)的javascript代碼 bilibili.js
// Variables used by Scriptable. // These must be at the very top of the file. Do not edit. // icon-color: pink; icon-glyph: tv; const MID = args.widgetParameter || '280793434'//UP主MID,即個(gè)人空間網(wǎng)址后面那段數(shù)字 https://space.bilibili.com/280793434 let apiUrl = "http://api.bilibili.com/x/relation/stat?vmid="+MID let data = await loadData() let widget = await createWidget(data) if (config.runsInApp) { widget.presentSmall(); } Script.setWidget(widget) Script.complete() async function createWidget(data) { let widget = new ListWidget() widget.backgroundColor = Color.white() let header = widget.addStack() let icon = header.addImage(await loadImage('https://www.bilibili.com/favicon.ico')) icon.imageSize = new Size(15, 15) header.addSpacer(10) let title = header.addText("嗶哩嗶哩粉絲") title.textColor = Color.black() title.textOpacity = 0.9 title.font = Font.lightSystemFont(14) widget.addSpacer(20) let flTxt = widget.addText(toThousands(data.data['follower'])) flTxt.textColor = new Color("#fb7299") flTxt.font = Font.boldRoundedSystemFont(getFontSize(data.data['follower'])) flTxt.centerAlignText() widget.addSpacer(20) let utTxt = widget.addText('更新于:'+nowTime()) utTxt.textColor = Color.black() utTxt.font = Font.systemFont(12) utTxt.textOpacity = 0.5 utTxt.centerAlignText() widget.url = 'bilibili://' return widget } async function loadData() { let req = new Request(apiUrl) req.allowInsecureRequest = true let json = await req.loadJSON() console.log(json) return json } async function loadImage(imgUrl) { console.log(imgUrl) let req = new Request(imgUrl) req.allowInsecureRequest = true let image = await req.loadImage() return image } function toThousands(num) { return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); } function nowTime(){ let date = new Date() return date.toLocaleTimeString('chinese', { hour12: false }) } function getFontSize(num){ if(num<99){ return 38 }else if(num<9999 && num>100){ return 30 }else if(num<99999 && num>10000){ return 28 }else if(num<999999 && num>100000){ return 24 }else if(num<9999999 && num>1000000){ return 22 }else{ return 20 } }
修改上面代碼的MID和UPNAME參數(shù),改為你需要關(guān)注的UP主信息。然后把上面的代碼文件通過(guò)iCould復(fù)制到Scriptable文件夾內(nèi),打開(kāi)APP就能看到代碼了。點(diǎn)擊可運(yùn)行查看效果。
另外你也可以點(diǎn)擊Scriptable APP右上角的加號(hào),新建一個(gè)代碼,然后復(fù)制代碼到編輯框保存即可。
把小組件添加到桌面
1.長(zhǎng)按桌面空白的地方,點(diǎn)擊左上角的加號(hào)。
2.在列表里找到Scriptable,然后點(diǎn)擊添加第一個(gè)小尺寸的組件。
3.這時(shí)桌面就出現(xiàn)一個(gè)空白的Scriptable小組件,點(diǎn)擊這個(gè)組件,在彈窗的界面里面點(diǎn)擊“選取”,然后找到你剛剛上傳的腳本,Parameter處填入你B站的MID即可。
小組件運(yùn)行效果
另外要說(shuō)明一點(diǎn),iOS的小組件不是實(shí)時(shí)更新的,應(yīng)該有個(gè)更新機(jī)制,但我沒(méi)有深入了解。
Scriptable這個(gè)APP還挺好玩的,我另外還做個(gè)小組件顯示網(wǎng)站的實(shí)時(shí)訪(fǎng)問(wèn)數(shù)據(jù)。有興趣進(jìn)一步了解的朋友請(qǐng)看文檔。