CanadaHelps fundraising donations totals

Adds total rows to the admin fundraising page, donations tab

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         CanadaHelps fundraising donations totals
// @namespace    https://greasyfork.org/users/1
// @version      1.1
// @description  Adds total rows to the admin fundraising page, donations tab
// @author       Jason Barnabe
// @match        https://www.canadahelps.org/en/pages/*/edit/
// @grant        none
// @license      GPL3
// ==/UserScript==

(function() {
    'use strict';

    let donationRoot = document.querySelector("#donations-pane h2 + table")
    if (!donationRoot) {
        return
    }

    let groupedTotals = {}
    let sum = Array.from(donationRoot.querySelectorAll("tbody tr:not(.message-row)")).forEach((row) => {
        let name = row.querySelector('.name').innerText
        let amount = parseFloat(row.querySelector('.amount').innerText.replace('$', ''))
        groupedTotals[name] ||= 0.0
        groupedTotals[name] += amount
    })

    let tfoot = document.createElement("tfoot")
    tfoot.style.borderTop = '4px solid black'

    Object.entries(groupedTotals).forEach(([key, value]) => {
        tfoot.appendChild(createFooterRow(key, value))
    })

    let totalRow = createFooterRow('TOTAL', Object.values(groupedTotals).reduce((sum, el) => sum + el))
    totalRow.style.borderTop = '4px solid black'
    tfoot.appendChild(totalRow)

    donationRoot.appendChild(tfoot)
})();

function createFooterRow(name, value) {
    let row = document.createElement("tr")
    row.appendChild(createNameCell(name))
    row.appendChild(document.createElement("td"))
    row.appendChild(createSumCell(value))
    row.appendChild(document.createElement("td"))
    return row
}

function createNameCell(name) {
    let nameCell = document.createElement("td")
    nameCell.classList.add('charity')
    nameCell.style.fontWeight = 'bold'
    nameCell.innerText = name
    return nameCell
}

function createSumCell(value) {
    let sumCell = document.createElement("td")
    sumCell.classList.add('amount')
    sumCell.innerText = "$" + value.toFixed(2)
    return sumCell
}