Add/Edit: add Last Service button to some dates

Add a button that sets its to the closest past Sunday or Wednesday, in the fields that are usually set to last service.

目前為 2025-12-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Add/Edit: add Last Service button to some dates
// @namespace    https://github.com/nate-kean/
// @version      2025.12.8
// @description  Add a button that sets its to the closest past Sunday or Wednesday, in the fields that are usually set to last service.
// @author       Nate Kean
// @match        https://jamesriver.fellowshiponego.com/members/edit/*
// @match        https://jamesriver.fellowshiponego.com/members/add*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=fellowshiponego.com
// @grant        none
// @license      MIT
// @run-at       document-end
// @require      https://update.greasyfork.org/scripts/554804/1689525/Nate%27s%20Day%20Button.js
// ==/UserScript==

(async function() {
    document.head.insertAdjacentHTML("beforeend", `
        <style id="nates-last-service-button-css">
            .dates-panel .date-section {
                max-width: 750px !important;

                & .nates-day-button.last-service {
                    float: right;
                    margin-top: -2px;
                }

                & .date-holder {
                    width: 250px !important;

                    & label {
                        width: unset !important;
                    }

                    & .input-holder {
                        width: 100%;
                    }
                }
            }
        </style>
    `);

    // Overrides
    const specialDates = [
        new Date("12/6/2025"), // James River Christmas Saturday 2025
    ];

    const fieldNames = [
        "First Visit Date",
        "Baptized",
        "Salvation Date",
        "Rededication Date",
    ];

	const Day = Object.freeze({
		SUNDAY: 0,
		MONDAY: 1,
		TUESDAY: 2,
		WEDNESDAY: 3,
		THURSDAY: 4,
		FRIDAY: 5,
		SATURDAY: 6,
	});

	function getLastWeekday(targetDate, day) {
		const currentWeekday = targetDate.getDay();
		const daysToSubtract = (currentWeekday - day + 7) % 7;
		const lastWeekdayDate = new Date(targetDate);
		lastWeekdayDate.setDate(targetDate.getDate() - daysToSubtract);
		return lastWeekdayDate;
	}

    function isSameDate(a, b) {
        return (
            a.getDate() === b.getDate()
            && a.getMonth() === b.getMonth()
            && a.getFullYear() === b.getFullYear()
        );
    }

    function getLastServiceDate(today, lastSunday, lastWednesday) {
        for (const date of specialDates) {
            if (
                today.getFullYear() === date.getFullYear()
                && today.getMonth() === date.getMonth()
                && today.getDate() === date.getDate()
            ) {
                return date;
            }
        }
        return new Date(Math.max(lastSunday, lastWednesday));
    }

    function delay(ms) {
        return new Promise((resolve) => setTimeout(resolve, ms));
    }


    const today = new Date();
    const lastSunday = getLastWeekday(today, Day.SUNDAY);
    const lastWednesday = getLastWeekday(today, Day.WEDNESDAY);
    const lastServiceDate = getLastServiceDate(today, lastSunday, lastWednesday);
    const buttonName = (isSameDate(today, lastServiceDate)) ? "Today" : "Last Service";
    const dateString = new Intl.DateTimeFormat("en-US", {
        month: "2-digit",
        day: "2-digit",
        year: "numeric",
    }).format(lastServiceDate);

    const yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    const dateStringYesterday = new Intl.DateTimeFormat("en-US", {
        month: "2-digit",
        day: "2-digit",
        year: "numeric",
    }).format(yesterday);

    for (const formGroup of document.querySelectorAll(".form-group")) {
        if (!fieldNames.includes(formGroup.querySelector("label")?.textContent.trim())) {
            continue;
        }
        const btn = document.createElement("button");
        btn.classList.add("nates-day-button", "last-service");
        btn.textContent = buttonName;
        btn.type = "button";
        btn.addEventListener("click", () => {
            formGroup.querySelector("input").value = dateString;
        }, { passive: true });
        formGroup.prepend(btn);
    }

    // TODO: use MutationObserver
    while (true) {
        for (const row of document.querySelectorAll(".memberEditGroupName > :nth-child(2 of div)")) {
            if (row.classList.contains("has-last-service-button")) continue;
            row.classList.add("has-last-service-button");

            const btn = document.createElement("button");
            btn.classList.add("nates-day-button", "last-service");
            btn.textContent = buttonName;
            btn.type = "button";
            btn.addEventListener("click", () => {
                row.querySelector("input").value = dateString;
            }, { passive: true });
        }
        await delay(100);
    }
})();