Last active
April 27, 2023 15:21
-
-
Save ttt50966/e1db8141cb0bfcc89f0dc285958a21bc to your computer and use it in GitHub Desktop.
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
// 定義 API 網址 | |
let url = "https://mainnet-node.like.co/likechain/likenft/v1/ranking"; | |
// 取得使用者設定的天數(預設為 2 天) | |
let daysAgo = args.widgetParameter || 2; | |
// 設定顯示的最大項目數 | |
let maxItems = 10; | |
// 準備 API 請求的 URL | |
let req = new Request(url + "?created_after=" + Math.floor(Date.now() / 1000 - 60 * 60 * 24 * daysAgo)); | |
// 發送 API 請求,取得回應 | |
let res = await req.loadJSON(); | |
// 從回應中取出需要的資料 | |
let items = res.classes | |
.map(item => { | |
// 將創建時間轉換成 JavaScript 的時間格式 | |
return { | |
...item, | |
createdAt: Date.parse(item.created_at), | |
}; | |
}) | |
// 根據創建時間排序,並只取前 maxItems 個項目 | |
.sort((a, b) => b.createdAt - a.createdAt) | |
.slice(0, maxItems); | |
// 創建小工具的畫面 | |
let widget = new ListWidget(); | |
// 逐一處理每個項目,加入到小工具中 | |
for (let item of items) { | |
let name = item.name; | |
// 如果項目名稱以 "Writing NFT - " 開頭,將其刪除 | |
if (name.startsWith("Writing NFT - ")) { | |
name = name.substr(14); | |
} | |
// 設定顯示項目的顏色和點擊後的連結 | |
let color = new Color("#BBA472"); | |
let nameText = widget.addText("▲ "+name); | |
nameText.url = "https://liker.land/nft/class/" + item.id; | |
nameText.textColor = color; | |
widget.addSpacer(4); | |
} | |
// 設定小工具的背景顏色 | |
widget.backgroundColor = new Color('2D6563') | |
// 顯示小工具 | |
widget.presentLarge(); | |
// 更新小工具的內容 | |
Script.setWidget(widget); | |
// 結束腳本執行 | |
Script.complete(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment