Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created May 1, 2022 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuadFlask/089f8eb40ca1d4120cb5845ccf447d56 to your computer and use it in GitHub Desktop.
Save QuadFlask/089f8eb40ca1d4120cb5845ccf447d56 to your computer and use it in GitHub Desktop.
const url = 'https://www.lego.com/ko-kr/product/horizon-forbidden-west-tallneck-76989';
const stockSelector = "#main-content > div > div.ProductDetailsPagestyles__ProductOverviewContainer-sc-1waehzg-1.dgDnXa > div > div.ProductOverviewstyles__Container-sc-1a1az6h-2.hHubKC > div.ProductOverviewstyles__PriceAvailabilityWrapper-sc-1a1az6h-10.bwcpjP > p > span";
const titleSelector = "#main-content > div > div.ProductDetailsPagestyles__ProductOverviewContainer-sc-1waehzg-1.dgDnXa > div > div.ProductOverviewstyles__Container-sc-1a1az6h-2.hHubKC > div.ProductOverviewstyles__ProductOverviewRow-sc-1a1az6h-1.hblOYO > h1 > span";
async function loadWeb(url, selectors) {
const wv = new WebView();
wv.loadURL(url);
await wv.waitForLoad();
return await wv.evaluateJavaScript(`
[${selectors.map(s => `"${s}"`).join(',')}].map(selector => document.querySelector(selector).innerText)
`, false);
}
const texts = await loadWeb(url, [stockSelector, titleSelector]);
const stockStatus = texts[0];
const title = texts[1];
const now = new Date();
const date = `${now.getMonth()+1}월 ${now.getDate()}일 ${now.toLocaleString('kr', { hour: 'numeric', minute: 'numeric', hour12: true })}`;
const getIcon = async (iconName, color = 'black') => {
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(`${dir}`, `${iconName}.png`)
if (fm.fileExists(path) && config.runsInWidget) {
return fm.readImage(path)
} else {
let iconImage = await loadImage(`https://iconsdb.com/icons/download/${color}/${iconName}.png`)
fm.writeImage(path, iconImage)
return iconImage
}
}
const loadImage = async (imageUrl) => {
let request = new Request(imageUrl)
return await request.loadImage()
}
const main = async () => {
let widget = new ListWidget()
widget.setPadding(30, 0, 30, 0)
widget.url = url
widget.backgroundColor = new Color('black')
widget.refreshAfterDate = new Date(Date.now() + 1000 * 3600 * 2) // 2시간 후 다시 업데이트
widget.addSpacer(2)
let titleTxt = widget.addText(title)
titleTxt.centerAlignText()
titleTxt.textColor = Color.white()
titleTxt.font = Font.boldRoundedSystemFont(12)
widget.addSpacer()
let countTxt = widget.addText(stockStatus)
countTxt.centerAlignText()
countTxt.textColor = stockStatus === "일시품절" ? Color.orange() : Color.white()
countTxt.font = stockStatus === "일시품절" ? Font.thinSystemFont(16) : Font.boldRoundedSystemFont(16)
widget.addSpacer()
let dateTxt = widget.addText(date)
dateTxt.centerAlignText()
dateTxt.textColor = Color.white()
dateTxt.font = Font.thinSystemFont(12)
widget.addSpacer(2)
return widget
}
// Main Start
let widget = await main()
if (config.runsInWidget) {
Script.setWidget(widget)
} else {
// for Test
widget.presentSmall()
}
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment