Widget Examples

Here’s the 09/2021 ‘update’:

/*************************
 * eqsOne 09/2021
 * Corona-Ampel Berlin
 *
 * https://data.lageso.de
/*************************/

let widget = new ListWidget()
widget.setPadding(16, 16, 16, 16)

const spc = 3

// Tag/Nacht für Berlin
var LAT = 52.50
var LON = 13.37
const date = new Date()
const sunData = await new Request("https://api.sunrise-sunset.org/json?lat=" + LAT + "&lng=" + LON + "&formatted=0&date=" + date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate()).loadJSON();
let now = date.getTime()
let sunrise = new Date(sunData.results.sunrise).getTime()
let sunset = new Date(sunData.results.sunset).getTime()
let night = (now < sunrise) || (now > sunset)

//Title Text
let titleTxt = widget.addText("Covid Berlin")
titleTxt.font= Font.boldSystemFont(17)
titleTxt.leftAlignText()
widget.addSpacer(spc)

//Value Text
let vlFnt = Font.semiboldSystemFont(20)

//Untertitel Textstyle
let ptFnt = Font.systemFont(8)
let ptCol

//Hintergrund
if (night) {
  titleTxt.textColor = Color.lightGray()
  ptCol = Color.gray()
  const gradient = new LinearGradient()
  gradient.locations = [0, 1]
  gradient.colors = [
    new Color("19191b"),
    new Color("1f1f1b")
  ]
  widget.backgroundGradient = gradient
}
else {
  titleTxt.textColor = Color.darkGray()
  ptCol = Color.darkGray()
}

await loadCnt()

if (!config.runsInWidget) widget.presentSmall()
Script.setWidget(widget)
Script.complete()

async function loadCnt() {
  let url='https://data.lageso.de/lageso/corona/corona.html'
  let wbv = new WebView()
  await wbv.loadURL(url)
  let jsc = `
  var arr = new Array()
  var nin = document
    .getElementById("neuinfektionen")
    .getElementsByTagName("p")[0]
    .innerText
  arr.push(nin)
  var hos = document
    .getElementById("hosp_7TI")
    .getElementsByTagName("p")[0]
    .innerText
  arr.push(hos)
  var bet = document
    .getElementById("its")
    .getElementsByTagName("p")[0]
    .innerText
  arr.push(bet)
  var gc1 = document
    .getElementById("neuinfektionen")
    .style
    .backgroundColor
  arr.push(gc1)
  var gc2 = document
    .getElementById("hosp_7TI")
    .style
    .backgroundColor
  arr.push(gc2)
  var gc3 = document
    .getElementById("its")
    .style
    .backgroundColor
  arr.push(gc3)
  JSON.stringify(arr)
  `
  let jsn = await wbv.evaluateJavaScript(jsc)
  let val = JSON.parse(jsn)
  
  let inf = val[0]
  let hos = val[1]
  let its = val[2]
  let co1 = val[3]
  let co2 = val[4]
  let co3 = val[5]
  
  let cc1 = toHEX(co1)
  let cc2 = toHEX(co2)
  let cc3 = toHEX(co3)
  
  function toHEX(col) {
    var a = col.split("(")[1].split(")")[0].replaceAll(" ", "")
    a = a.split(",")
    var b = a.map(function(x) {
    x = parseInt(x).toString(16)
    return (x.length==1) ? "0"+x : x
    })
    b = "0x"+b.join("")
    b = b.substring(2)
    return b
  }
  
//**************************
  
  //Inzidenz Text
  if (inf != null) {
    let tx2 = widget.addText(inf)
      tx2.leftAlignText()
      tx2.font = vlFnt
      tx2.textColor = new Color(cc1)
  }
  //Inzidenz Untertitel
  let tx1 = widget.addText("7-Tage Inzidenz")
  tx1.textColor = ptCol
  tx1.font= ptFnt
  tx1.leftAlignText()
  widget.addSpacer(spc)
  
  //Hospitalisierung Text
  if (hos != null) {
    let tx4 = widget.addText(hos)
      tx4.leftAlignText()
      tx4.font = vlFnt
      tx4.textColor = new Color(cc2)
  }
  //Hospitalisierung Untertitel
  let tx3 = widget.addText("Hospitalisierungs 7-T. Inz.")
  tx3.textColor = ptCol
  tx3.font= ptFnt
  tx3.leftAlignText()
  widget.addSpacer(spc)
  
  //Intensivbetten Text
  if (its != null) {
    let tx6 = widget.addText(its)
      tx6.leftAlignText()
      tx6.font = vlFnt
      tx6.textColor = new Color(cc3)
  }
  //Intensivbetten Untertitel
  let tx5 = widget.addText("ITS-Belegung")
  tx5.textColor = ptCol
  tx5.font= ptFnt
  tx5.leftAlignText()
}