Groups: Fix CSV Export "Take Action On" setting

Fix a bug that causes F1 to ignore your "Take action on" setting in the CSV Export in this page. I.e., you can export parents from this page now.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Groups: Fix CSV Export "Take Action On" setting
// @namespace    https://github.com/nate-kean/
// @version      2025-09-30
// @description  Fix a bug that causes F1 to ignore your "Take action on" setting in the CSV Export in this page. I.e., you can export parents from this page now.
// @author       Nate Kean
// @match        https://jamesriver.fellowshiponego.com/groups*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=fellowshiponego.com
// @grant        none
// @license      MIT
// ==/UserScript==


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

    async function waitForElement(selector, pollingRateMs=100, parent=document) {
        let el;
        while (true) {
            el = parent.querySelector(selector);
            if (el) return el;
            await delay(pollingRateMs);
        }
    }

    async function elementGone(selector, pollingRateMs=100, parent=document) {
        let el;
        while (true) {
            el = parent.querySelector(selector);
            if (!el) return;
            await delay(pollingRateMs);
        }
    }

    function eventFired(eventName, el=document) {
        return new Promise((resolve) => {
            let listener;
            listener = el.addEventListener(eventName, (evt) => {
                resolve(evt);
                el.removeEventListener(eventName, listener);
            });
        });
    }

    // In a loop because the form has to be patched every time you open it
    while (true) {
        const form = await waitForElement("#commonActionForm");

        // Add the missing parameters to the form
        const uidSelector = form.querySelector("input[name='uidSelector']");
        const takeActionOn = document.createElement("input");
        takeActionOn.type = "hidden";
        takeActionOn.name = "takeActionOn";
        form.appendChild(takeActionOn);

        // Wire them up to be included in the form
        const dropdown = form.querySelector("#peopleListOption");
        form.addEventListener("submit", () => {
            uidSelector.value = takeActionOn.value = dropdown.selectedOptions[0].value;
            return true;
        });

        // Fix the incorrect internal names of the dropdown options
        for (const option of dropdown.querySelectorAll("option")) {
            switch (option.value) {
                case "":
                    option.value = "1LPI"; break;
                case "parents":
                    option.value = "Parents"; break;
                case "children":
                    option.value = "Children"; break;
                case "both":
                    option.value = "ParentsAndChildren"; break;
                case "all":
                    option.value = "AllFamilyMembers"; break;
                case "commParents":
                    option.value = "ParentsForCommunication"; break;
            }
        }
        await eventFired("submit", form);
    }
})();