Last active
March 17, 2021 05:26
-
-
Save BrennanMcDonald/1ce0d486cd736e6e2820e5812742a8af to your computer and use it in GitHub Desktop.
Adds an extra column to covid tracker
This file contains 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
// ==UserScript== | |
// @name Covid19 Add Column | |
// @namespace http://brennan.sh/ | |
// @version 1.0 | |
// @description Adds a column of statistics to covid19vaccine tracker | |
// @author Brennan | |
// @match https://covid19tracker.ca/vaccinationtracker.html | |
// @icon https://www.google.com/s2/favicons?domain=covid19tracker.ca | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const CANADA_POPULATION = 37590000; | |
const NEW_HEADER_TEXT = 'Percent who have recieved at least one dose'; | |
const VACCINE_PROVINCE_TABLE_ID = 'vaccinationsProvinceTable'; | |
const DATA_TABLE_ID = 'dataTable2'; | |
const formatPercentString = (percent) => { | |
return (((percent).toString() !== 'NaN') ? `${(percent).toString()}%` : 'N/A') | |
}; | |
const populationDict = { | |
'Ontario': 14570000, | |
'Quebec': 8485000, | |
'British Columbia': 5071000, | |
'Alberta': 4370000, | |
'Manitoba': 1369000, | |
'Saskatchewan': 1174000, | |
'New Brunswick': 776827, | |
'Nova Scotia': 971395, | |
'Newfoundland and Labrador': 521542, | |
'Northwest Territories': 44826, | |
'Yukon': 35874, | |
'Nunavut': 38780, | |
'Prince Edward Island': 156947, | |
'Canada': 37590000, | |
}; | |
const createTableCell = (textContent, parent, element = 'td') => { | |
const newCol = document.createElement(element); | |
newCol.textContent = textContent | |
parent.appendChild(newCol); | |
}; | |
const render = () => { | |
const vaccinationsProvinceTable = document.getElementById(VACCINE_PROVINCE_TABLE_ID); | |
if (vaccinationsProvinceTable.children.length !== 0) { | |
const dataTable2 = document.getElementById(DATA_TABLE_ID); | |
const totalDosesCanada = parseInt(dataTable2.children[1].children[0].children[1].textContent.split(' ')[0].replaceAll(',', '')); | |
const fullyVaccinatedCanada = parseInt(dataTable2.children[1].children[0].children[5].textContent.split(' ')[0].replaceAll(',', '')); | |
const percentCanada = Math.round(((totalDosesCanada - fullyVaccinatedCanada) / CANADA_POPULATION) * 10000) / 100; | |
for (let i = 0; i < vaccinationsProvinceTable.children.length; i++) { | |
const parentRow = vaccinationsProvinceTable.children[i]; | |
const province = parentRow.children[0].textContent.trim(); | |
const totalDoses = parseInt(parentRow.children[1].textContent.split(' ')[0].replaceAll(',', '')); | |
const fullyVaccinated = parseInt(parentRow.children[5].textContent.split(' ')[0].replaceAll(',', '')); | |
const rowPercent = Math.round(((totalDoses - fullyVaccinated) / populationDict[province]) * 10000) / 100; | |
createTableCell(formatPercentString(rowPercent), vaccinationsProvinceTable.children[i]); | |
} | |
createTableCell(NEW_HEADER_TEXT, dataTable2.children[0].children[0], 'th'); | |
createTableCell(formatPercentString(percentCanada), dataTable2.children[1].children[0]); | |
// Only run once after table has been populated. | |
clearInterval(interval); | |
} | |
} | |
render(); | |
const interval = setInterval(render, 500); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment