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-11-23 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Add/Edit: add Last Service button to some dates
// @namespace    https://github.com/nate-kean/
// @version      2025.11.23.1
// @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==

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

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

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

                    & label {
                        width: unset !important;
                    }

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

    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()
        );
    }


    const today = new Date();
    const lastSunday = getLastWeekday(today, Day.SUNDAY);
    const lastWednesday = getLastWeekday(today, Day.WEDNESDAY);
    const lastServiceDate = new Date(Math.max(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);

    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);
    }
})();