From e650c629b78b7fd28d3fc8c993417e32eb0c38d3 Mon Sep 17 00:00:00 2001 From: zongor Date: Tue, 5 Nov 2024 08:34:57 -0500 Subject: [PATCH] Add index.js --- index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..d837872 --- /dev/null +++ b/index.js @@ -0,0 +1,52 @@ +(() => { + let candidates; + let headerContainer = document.querySelector('.wrapper-power-bar'); + headerContainer.insertAdjacentHTML('afterend', `
NameVotes
`); + const { + fetch: originalFetch + } = window; + window.fetch = async (...args) => { + let [resource, config] = args; + let response = await originalFetch(resource, config); + if (!response.ok) { + return response; + } + const f = response.url.split('/').pop(); + if (f === "metadata.json") { + const metadata = await response.json(); + candidates = new Map(metadata.candidates.map((c) => [c.uid, c.fullName])); + } else if (f === "president.json") { + + const president = await response.json(); + let data = president[0]["state_electionTypes"].map((t) => t.state.officeRaces[0].candidateVotes.map((o) => { + return { + "name": (candidates) ? candidates.get(o.candidate) : o.candidate, + "votes": o.totalVote + }; + })).reduce((acc, current) => { + current.forEach((candidate) => { + const existingCandidate = acc.find((c) => c.name === candidate.name); + if (existingCandidate) { + existingCandidate.votes += candidate.votes; + } else { + acc.push(candidate); + } + }); + return acc; + }, []); + + if (data.length > 0) { + let tableBody = document.getElementById('table-body'); + tableBody.innerHTML = ''; + data.forEach((row, index) => { + let rowElement = tableBody.insertRow(); + let nameCell = rowElement.insertCell(); + let votesCell = rowElement.insertCell(); + nameCell.textContent = row.name; + votesCell.textContent = row.votes; + }); + } + } + return response; + }; +})(); \ No newline at end of file