Extract news headlines and display in table


#1

This script will find headlines from danish soccer website www.bold.dk.
The pieces of news are then rendered in a UITable and has an onTab on each link, allowing you to visit the piece of news to read the rest.

Sharing so that others can be inspired :slight_smile:

    {
  "always_run_in_app" : false,
  "icon" : {
    "color" : "green",
    "glyph" : "futbol"
  },
  "name" : "News from bold.dk",
  "script" : "const webview = new WebView();\nawait webview.loadURL(\"https:\/\/bold.dk\");\n\nvar getData = `\n  var rep = ['ae','æ','aa','å','oe','ø','\\\\\/',''];\n  function clean(s) {\n    for(var r=0; r<rep.length-1; r+=2) {\n      s = s.replace(new RegExp( rep[r], 'g'), rep[r+1])\n    }\n    return s;\n  }\n  function runit() {\n    const links = Array.from(document.querySelectorAll('a[href*=\"nyheder\"]')).map((html, i) => {\n      const href = html.toString();\n      let text = href.split('https:\/\/www.bold.dk\/fodbold\/nyheder\/')[1];\n      if (text.trim().length !== 0) {\n        let time = html.innerText;\n        const l = time.length;\n        time = time.substr(l-5, 5);\n        text = clean(text).split('-').join(' ');\n        text = text.substr(0, 1).toUpperCase()+text.substr(1, text.length-1);\n        return {href, text, time}\n      }\n    });\n    return links.filter(obj => obj);\n  }\n\n  runit();\n`\n\nlet response = await webview.evaluateJavaScript(getData, false);\n\nconst table = new UITable();\nresponse.map(row => {\n  const {text, href, time} = row;\n  if (time.indexOf(':') === 2) {\n    const TR = new UITableRow();\n    const TDTime = UITableCell.text(time)\n    const TDText = UITableCell.text(text)\n    TDTime.widthWeight = 10;\n    TDText.widthWeight = 80;\n    TR.addCell(TDTime);\n    but = TR.addButton(\"Læs mere\").onTap = () => {\n      Safari.open(href);\n    };\n    TR.addCell(TDText);\n\n    table.addRow(TR);\n  }\n});\n\ntable.present();",
  "share_sheet_inputs" : [

  ]
}

#2

Thanks for sharing :blush: