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-04 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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

(async function() {
    document.head.insertAdjacentHTML("beforeend", `
        <style id="nates-day-button-css">
            .nates-day-button {
                float: right;
                font-weight: 600;
                border: none;
                font-size: 13px;
                margin-top: -2px;
                padding: 0;
            }

            .dates-panel .date-section {
                max-width: 700px !important;

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

                    & label {
                        width: unset !important;
                    }

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

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

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

	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 getDateString() {
        const today = new Date();
        const lastSunday = getLastWeekday(today, Day.SUNDAY);
        const lastWednesday = getLastWeekday(today, Day.WEDNESDAY);
        const lastServiceDate = Math.max(lastSunday, lastWednesday)
        return 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");
        btn.textContent = "Last Service";
        btn.type = "button";
        btn.addEventListener("click", () => {
            formGroup.querySelector("input").value = getDateString();
        }, { passive: true });
        formGroup.prepend(btn);
    }
})();