Skip to content

Instantly share code, notes, and snippets.

@supermamon
Created September 28, 2021 02:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supermamon/c29726374142664d1f139015801faaf7 to your computer and use it in GitHub Desktop.
Save supermamon/c29726374142664d1f139015801faaf7 to your computer and use it in GitHub Desktop.
The Golf Club Widget
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: globe-africa;
/* **********************************************
The Golf Club Wdiget
By: @supermamon
On: 2021-09-28
For: reddit.com/r/Scriptable/comments/pwnrq5/leaderboard_table_formatted_for_ios_widget/
********************************************** */
// preferences
const FONT_SIZE = 11
const EXCLUDE_HC = true
const HEADER_COLOR = Color.red()
const ROW_COLOR = Color.blue()
const BACKGROUND_COLOR = Color.black()
const url = 'https://tgccloud.com/2019/reports/lblive19.php'
const font = Font.regularSystemFont(FONT_SIZE)
// adjust only if preferred
const colWidths = [120,45,45,45,45,45]
// get the data
const wv = new WebView()
await wv.loadURL(url)
//await wv.present()
const js = `
const headers = Array.from(document.querySelectorAll('.xcrud-th th')).map( node => node.textContent )
const rows = Array.from(document.querySelectorAll('.xcrud-row')).map( row => Array.from(row.childNodes).map(cell => cell.textContent.replace(/^\\s+/,'')) )
const ret = {
headers: headers,
rows: rows
}
ret
`
const data = await wv.evaluateJavaScript(js,false)
const widgetFamily = config.widgetFamily ? config.widgetFamily : 'large'
const widget = new ListWidget()
widget.backgroundColor = BACKGROUND_COLOR
// add the headers
const headerStack = widget.addStack()
headerStack.layoutHorizontally()
let idx = -1
for (header of data.headers) {
idx++
if (EXCLUDE_HC && idx==4) continue;
const headerText = header
const headerCell = headerStack.addStack()
headerCell.layoutHorizontally()
headerCell.backgroundColor = HEADER_COLOR
headerCell.size = new Size(colWidths[idx],0)
const textElement = headerCell.addText(headerText)
textElement.font = font
headerStack.addSpacer(2) // between cells
}
// add the data
// limit the number of rows based on widget size
const rows = data.rows.slice(0, widgetFamily=='large'?10:5)
for (const row of rows) {
const rowStack = widget.addStack()
rowStack.layoutHorizontally()
let idx = -1
for (const cell of row) {
idx++
if (EXCLUDE_HC && idx==4) continue;
const cellText = cell
const dataCell = rowStack.addStack()
dataCell.layoutHorizontally()
dataCell.backgroundColor = Color.blue()
dataCell.size = new Size(colWidths[idx],0)
const textElement = dataCell.addText(cellText)
textElement.font = font
rowStack.addSpacer(2) // between cells
}
widget.addSpacer(1) // between rows
}
if (config.runsInApp) await widget[`present${widgetFamily.split('').map( (c,i)=>(i==0?c.toUpperCase():c)).join('')}`]()
Script.setWidget(widget)
Script.complete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment