Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // javascript:void function(){var a,b={Alberta:"Severe Outcomes table"}.Alberta,c={Alberta:"#severe-outcomes"}.Alberta||"",d={Alberta:`alberta.ca/stats/covid-19-alberta-statistics.htm${c}`}.Alberta,e={Alberta:"#main #summary tr"}.Alberta,f={Alberta:function(a){var b=t(a[0]);if(10<a.length&&"Unknown"!=b){var c=m.length;m[c]=b.replace(/ years?/,"").replace(/Total/,"OVERALL"),n[c]=1*t(a[8]),o[c]=1*t(a[1]),p[c]=w(n[c],o[c]),q[c]=x(p[c]),r[c]=1-p[c]}}}.Alberta,g={Alberta:/^(?:Under (\d)|(\d+)\+|(\d+)-(\d+))$/}.Alberta,h={Alberta:0}.Alberta,j={Alberta:1}.Alberta,k={Alberta:3}.Alberta,l={Alberta:function(a,b){return a<b[1]||a>=b[2]||a>=b[k]&&a<=b[k+1]}}.Alberta,m=[],n=[],o=[],p=[],q=[],r=[],s=function(a,b){return(b||document).querySelectorAll(a)},t=function(a,b){return a&&a[b||"innerText"]},u=function(a,b){return`${a} ${b}${1==a?"":"s"}`},v=function(b,c){return isNaN(parseInt(b))?c===a?-1:c:parseInt(b)},w=function(a,b){return Math.max(a/b,0)||0},x=function(a){return a&&`1 in ${1/a}`||"ZERO"},y=function(){s(e).forEach(function(a){f(s("td",a))})},z=function(a){var b=!1,c=v(a);return 0>c?b:(m.forEach(function(a,d){var e=a.match(g)||[];!1===b&&(b=l(c,e)&&d)}),b)},A=function(a,b,c,d,e,f){return`${a}:\t${u(b,"death")} in ${u(c,"case")} = CFR ${100*d}%\n(risk: ${e}, survival rate ${100*f}%)`},B=function(a){var b="",e=m[a].match(g)||[],f=1*e[k+1]+1||0;if(f){for(var h,l=0,p=0,q=j;q<=a;q++)l+=n[q],p+=o[q];b=` ; \n\n${A(`Under ${f}`,l,p,h=w(l,p),x(h),1-h)}`}return b},C=function(a){var b=A(m[a],n[a],o[a],p[a],q[a],r[a]),c=B(a);return`\n${b}${c}\n`},D=function(b,c){var d=v(z(b),h),e=C(d),f=e.replace(/[\t ;]+/g," "),g=e.replace(/[\t \n]+/g," ").trim();return console.log(`${e.replace(";","")}\n`),e=c===a?prompt(f,g):null,null==e?-1:b};(function(c){if(c===a&&0>location.href.indexOf(d))return alert(`\nTry again after ${b} has loaded:\n\n${d}`),location.href=`https://${d}`;y();for(var e=0;0<=v(e);){var f=c===a?prompt("\nCOVID19 risk/survival based on Alberta stats:\n\nenter AGE [Cancel to quit]\n"):c;e=D(f,c)}})()}();
- // https://pastebin.com/TAfjQdth = "Covid19 Risk By Age (Alberta) - bookmarklet (javascript)"
- // TODO: adapt to other "where" values, to process the other major provinces as well:
- // BC, Ontario, Quebec, Sask, Manitoba -- haven't looked into how easy it is to grab and summarize similar data from their Official Covid Stats websites' tables...
- // Maybe USA states too? esp. NY, CA, FL, TX
- void function(){
- var where = "Alberta"
- , pageName = {
- Alberta: "Severe Outcomes table"
- }[where]
- , hash = {
- Alberta: "#severe-outcomes"
- }[where] || ""
- , url = {
- Alberta: `alberta.ca/stats/covid-19-alberta-statistics.htm${hash}`
- }[where]
- , selSummaryRows = {
- Alberta: "#main #summary tr"
- }[where]
- , addRow = {
- Alberta: function(cells) {
- var ageGroup = txt(cells[0])
- if(cells.length > 10 && ageGroup != "Unknown") {
- var i = ageGroups.length;
- ageGroups[i] = ageGroup.replace(/ years?/, "").replace(/Total/, "OVERALL");
- deaths[i] = 1 * txt(cells[8]);
- cases[i] = 1 * txt(cells[1]);
- CFR[i] = div(deaths[i], cases[i]); // {Alberta: sanityTestCFRpercent[i] = txt(cells[9])}
- chance[i] = getChance(CFR[i]);
- survival[i] = 1 - CFR[i]
- }
- }
- }[where]
- , reGroupLabels = {
- Alberta: /^(?:Under (\d)|(\d+)\+|(\d+)-(\d+))$/
- }[where]
- , groupIndexOverall = {
- Alberta: 0
- }[where]
- , firstGroupIndexWithRange = {
- Alberta: 1
- }[where]
- , matchIndexForRange = {
- Alberta: 3
- }[where]
- , isAgeGroup = {
- Alberta: function(age, matches) { return age < matches[1] || age >= matches[2] || (age >= matches[matchIndexForRange] && age <= matches[matchIndexForRange + 1]); }
- }[where]
- , u = undefined
- , message = `\nCOVID19 risk/survival based on ${where} stats:\n\nenter AGE [Cancel to quit]\n`
- , ageGroups = [], deaths = [], cases = [], CFR = [], chance = [], survival = [] // , sanityTestCFRpercent = []
- , qs = function(selectors, el) { return (el || document).querySelectorAll(selectors); }
- , txt = function(el, attribute) { return el && el[attribute || "innerText"]; } // || "textContent"
- , plural = function(n, s) { return `${n} ${s}${n != 1 ? "s" : ""}`; }
- , num = function(v, def) { return isNaN(parseInt(v)) ? (def === u ? -1 : def) : parseInt(v); }
- , div = function(num, by) { return Math.max(num / by, 0) || 0; }
- , getChance = function(n) { return (n && `1 in ${1 / n}`) || "ZERO"; }
- , addRows = function() {
- qs(selSummaryRows).forEach(function(el, i) {
- addRow(qs("td", el));
- });
- }
- , getGroup = function(ageValue) {
- var result = false, age = num(ageValue);
- return age < 0 ? result : (ageGroups.forEach(function(label, i) {
- var matches = label.match(reGroupLabels) || [];
- if(result === false) {
- result = isAgeGroup(age, matches) && i;
- }
- }), result);
- }
- , getRisk = function(label, deaths, cases, CFR, chance, survival) {
- return `${label}:\t${plural(deaths, "death")} in ${plural(cases, "case")} = CFR ${100 * CFR}%\n(risk: ${chance}, survival rate ${100 * survival}%)`
- }
- , getSummaryUnder = function(groupIndex) {
- var result = ""
- , matches = ageGroups[groupIndex].match(reGroupLabels) || []
- , ageUnder = (1 * matches[matchIndexForRange + 1] + 1) || 0;
- if(ageUnder) {
- var d = 0, c = 0, rate;
- for(var i = firstGroupIndexWithRange; i <= groupIndex; i ++) {
- d += deaths[i]
- c += cases[i]
- }
- result = ` ; \n\n${getRisk(`Under ${ageUnder}`, d, c, rate = div(d, c), getChance(rate), 1 - rate)}`;
- }
- return result;
- }
- , getRiskForAgeGroupIndex = function(i) {
- var result = getRisk(ageGroups[i], deaths[i], cases[i], CFR[i], chance[i], survival[i])
- , summary = getSummaryUnder(i);
- return `\n${result}${summary}\n`
- }
- , revealRiskForAge = function(age, local) {
- var ageGroupIndex = num(getGroup(age), groupIndexOverall)
- , result = getRiskForAgeGroupIndex(ageGroupIndex)
- , msg = result.replace(/[\t ;]+/g, " ")
- , def = result.replace(/[\t \n]+/g, " ").trim();
- console.log(`${result.replace(";", "")}\n`);
- result = local === u ? prompt(msg, def) : null;
- return result == null ? -1 : age;
- }
- , main = function(local) {
- if(local === u && location.href.indexOf(url) < 0) {
- return alert(`\nTry again after ${pageName} has loaded:\n\n${url}`), location.href = `https://${url}`;
- }
- addRows();
- var age = 0;
- while(num(age) >= 0) {
- var result = local === u ? prompt(message) : local;
- age = revealRiskForAge(result, local);
- }
- };
- main(); // bookmarklet, with redirect before it calls prompt()
- // main(55); // no redirect, no prompt, no loop; one-time hard-coded age, on current page (usually local .MHT snapshot)
- }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement