Last active
November 25, 2020 14:20
-
-
Save Shawin/0c5d4a03bc6df48aaa7759cfeef8b31d to your computer and use it in GitHub Desktop.
USA Election 2020 Votes – Scriptable Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start with this | |
let widget = new ListWidget() | |
// | |
// Set colors | |
let bidenColor = new Color("1a68ff") | |
let trumpColor = new Color("ff4a43") | |
let bidenColor2 = new Color("1a68ff", 0.8) | |
let trumpColor2 = new Color("ff4a43", 0.8) | |
let mainStack = widget.addStack() | |
mainStack.layoutHorizontally() | |
if (config.widgetFamily == "small") { | |
mainStack.layoutVertically() | |
} | |
mainStack.centerAlignContent() | |
let leftStack = mainStack.addStack() | |
leftStack.centerAlignContent() | |
if (config.widgetFamily !== "small") { | |
mainStack.addSpacer() | |
} | |
let rightStack = mainStack.addStack() | |
rightStack.centerAlignContent() | |
let imgStackBiden = leftStack.addStack() | |
imgStackBiden.backgroundColor = bidenColor | |
imgStackBiden.cornerRadius = 35 | |
leftStack.addSpacer(10) | |
let textStackBiden = leftStack.addStack() | |
textStackBiden.layoutVertically() | |
let voteStackBiden = textStackBiden.addStack() | |
let nameStackBiden = textStackBiden.addStack() | |
let textStackTrump = rightStack.addStack() | |
textStackTrump.layoutVertically() | |
let voteStackTrump = textStackTrump.addStack() | |
let nameStackTrump = textStackTrump.addStack() | |
rightStack.addSpacer(10) | |
let imgStackTrump = rightStack.addStack() | |
imgStackTrump.backgroundColor = trumpColor | |
imgStackTrump.cornerRadius = 35 | |
await loadSite() | |
// end with this | |
if (!config.runsInWidget) widget.presentMedium() | |
Script.setWidget(widget) | |
Script.complete() | |
// | |
async function getImage(url) { | |
let req = new Request(url) | |
return await req.loadImage() | |
} | |
async function loadSite() { | |
let url='https://www.theguardian.com/us-news/ng-interactive/2020/nov/03/us-election-2020-live-results-donald-trump-joe-biden-who-won-presidential-republican-democrat' | |
let wbv = new WebView() | |
await wbv.loadURL(url) | |
//javasript to grab data from the website | |
let jsc = ` | |
var arr = new Array() | |
var bidenImage = document.getElementById("topline").getElementsByTagName("img")[0].getAttribute("src") | |
arr.push(bidenImage) | |
var bidenVotes = document.getElementById("topline").getElementsByClassName("ge-bar__count")[0].innerText | |
arr.push(bidenVotes) | |
var bidenName = document.getElementById("topline").getElementsByClassName("ge-bar__surname")[0].innerText | |
arr.push(bidenName) | |
var trumpName = document.getElementById("topline").getElementsByClassName("ge-bar__surname")[1].innerText | |
arr.push(trumpName) | |
var trumpVotes = document.getElementById("topline").getElementsByClassName("ge-bar__count")[1].innerText | |
arr.push(trumpVotes) | |
var trumpImage = document.getElementById("topline").getElementsByTagName("img")[3].getAttribute("src") | |
arr.push(trumpImage) | |
JSON.stringify(arr) | |
` | |
//Run the javascript | |
let jsn = await wbv.evaluateJavaScript(jsc) | |
//Parse the grabbed values into a variable | |
let val = JSON.parse(jsn) | |
//Assign the parts to single variables | |
let bidenImageURL = val[0] | |
let bidenVotes = val[1] | |
let bidenName = val[2] | |
let trumpName = val[3] | |
let trumpVotes = val[4] | |
let trumpImageURL = val[5] | |
let myImageSize = new Size(70, 70) | |
let fontBig = new Font("Georgia-Bold", 32) | |
let fontSmall = new Font("Georgia-Bold", 12) | |
if (config.widgetFamily == "small") { | |
fontBig = new Font("Georgia-Bold", 18) | |
fontSmall = new Font("Georgia-Bold", 8) | |
} | |
let bidenImage = await getImage(bidenImageURL) | |
let theBidenImage = imgStackBiden.addImage(bidenImage) | |
theBidenImage.imageSize = myImageSize | |
let bidenVotesText = voteStackBiden.addText(bidenVotes) | |
bidenVotesText.font = fontBig | |
bidenVotesText.textColor = Color.blue() | |
let bidenNameText = nameStackBiden.addText(bidenName) | |
bidenNameText.textColor = Color.blue() | |
bidenNameText.font = fontSmall | |
voteStackTrump.addSpacer() | |
let trumpVotesText = voteStackTrump.addText(trumpVotes) | |
trumpVotesText.font = fontBig | |
trumpVotesText.textColor = Color.red() | |
nameStackTrump.addSpacer() | |
let trumpNameText = nameStackTrump.addText(trumpName) | |
trumpNameText.textColor = Color.red() | |
trumpNameText.font = fontSmall | |
let trumpImage = await getImage(trumpImageURL) | |
let theTrumpImage = imgStackTrump.addImage(trumpImage) | |
theTrumpImage.imageSize = myImageSize | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment