Skip to content

Instantly share code, notes, and snippets.

@HorseCheng
Created October 5, 2020 15:10
Show Gist options
  • Save HorseCheng/ef6df48bf21160f50d8a63b4c33586bc to your computer and use it in GitHub Desktop.
Save HorseCheng/ef6df48bf21160f50d8a63b4c33586bc to your computer and use it in GitHub Desktop.
Scriptable app: PTT Widget for iOS14
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: black; icon-glyph: user-md;
let page="Gossiping"
let showline=15
let fontsize=15
let html= new Request("")
html.url="https://www.ptt.cc/bbs/"+page
html.headers={"cookie": "over18=1;"}
let htmlstr=await html.loadString()
// Search for 置底分界
let yy=[...htmlstr.matchAll(/[\s\S]+r-list-sep/g)]
// Search for all title in the latest page
let re= /title">\s+<a href="(.*?)">(.*?)<\/a>/g
let match= [...yy[0][0].matchAll(re)]
// console.log(match)
//Search for all title in last page
let lasturl=[...htmlstr.matchAll(/index(.+).html">&lsaquo; 上頁<\/a>/g)]
let lasthtml= new Request("")
lasthtml.url="https://www.ptt.cc/bbs/"+page+"/index"+lasturl[0][1]+".html"
lasthtml.headers={"cookie": "over18=1;"}
let lasthtmlstr=await lasthtml.loadString()
let lastre= /title">\s+<a href="(.*?)">(.*?)<\/a>/g
let lastmatch= [...lasthtmlstr.matchAll(lastre)]
// console.log(lastmatch)
// create and show widget
let widget = createWidget("PTT", match, lastmatch)
Script.setWidget(widget)
Script.complete()
if (!config.runsInWidget) {
await widget.presentLarge();
}
function createWidget(title, m, lastm) {
let w = new ListWidget()
w.backgroundColor = new Color("#1A1A1A")
let ending=0
let diffending=0
let diff=0
if (m.length-showline>0) ending=m.length-showline
else { diff=showline-m.length; diffending= lastm.length-diff}
//插入最新頁面結果
for (let i=m.length-1; i>=ending;i--){
let x=w.addStack()
let str=x.addText(m[i][2])
x.url="https://www.ptt.cc/"+m[i][1]
str.font = Font.boldSystemFont(fontsize)
str.leftAlignText()
}
//插入第二頁結果 (如果第一頁結果不夠多)
if (diff>0){
for (let i=lastm.length-1; i>=diffending;i--){
let stack=w.addStack()
let str=stack.addText(lastm[i][2])
stack.url="https://www.ptt.cc/"+lastm[i][1]
str.font = Font.boldSystemFont(fontsize)
str.leftAlignText()
}
}
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment