import CommonUtils from "../utils/CommonUtils"; import Log from "../Log"; import WuhuBase from "../WuhuBase"; import UserScriptEngine from "../../enum/UserScriptEngine"; import Popup from "../utils/Popup"; import STOCK_IMG_HTML from "../../static/html/stock_img.html"; import * as FILTER from "../../static/json/for_stock_item_filter.json"; import WindowActiveState from "./WindowActiveState"; export default class TravelItem extends WuhuBase { className = 'TravelItem'; private readonly apiUrl: string = 'https://yata.yt/api/v1/travel/export/'; private foreignStockInfo: any = null; public constructor() { super(); window.setInterval(async () => { if (!WindowActiveState.getInstance().get()) return; Log.info('fetching ', this.apiUrl); this.foreignStockInfo = JSON.parse(await CommonUtils.COFetch(this.apiUrl)); Log.info({ 'fetch returned': this.foreignStockInfo }); }, 30 * 1000); } // 呈现内容 public async clickHandler(): Promise { if (CommonUtils.getScriptEngine() === UserScriptEngine.RAW) { new Popup(STOCK_IMG_HTML.replace('{{}}', performance.now().toString()), '飞花库存'); } else { const popup = new Popup("请稍后 " + CommonUtils.loading_gif_html(), '飞花库存'); let table = ``; const dest = FILTER.default; const now = new Date(); const res = await this.get(); Log.info({ res }) if (!res || !res.stocks) return; dest.forEach(el => { const update = (now.getTime() - new Date(res.stocks[el.name]['update'] * 1000).getTime()) / 1000 | 0 table += ``; let count = 0; res.stocks[el.name]['stocks'].forEach(stock => { if (el.stocks[stock.name]) { table += `${ el.stocks[stock.name] } (${ stock['quantity'] })`; count++; } }); while (count < 3) { count++; table += ''; } table += ''; }); table += '
目的地 - 更新时间库存
${ el.show }${ update / 60 | 0 }分${ update % 60 | 0 }秒前
'; popup.getElement().innerHTML = table; } } private async get() { return this.foreignStockInfo ||= JSON.parse(await CommonUtils.COFetch(this.apiUrl)); } }