Yokohama Facility Reservation - Select All

Adds "Select All" and "Deselect All" buttons to the Yokohama facility reservation page.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @license MIT
// @name         Yokohama Facility Reservation - Select All
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds "Select All" and "Deselect All" buttons to the Yokohama facility reservation page.
// @author       Gemini
// @match        https://www.shisetsu.city.yokohama.lg.jp/user/AvailabilityCheckApplySelectFacility
// @grant        none
// @icon         https://www.google.com/s2/favicons?sz=64&domain=yokohama.lg.jp
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Finds the target location and inserts the control buttons.
     * @returns {boolean} - True if the buttons were added successfully, false otherwise.
     */
    function addControlButtons() {
        // Find the container where the page title is located
        const headerContainer = document.querySelector('.page-header .d-flex');

        // If the container isn't on the page yet, or if we've already added the button, do nothing.
        if (!headerContainer || document.getElementById('selectAllBtn-userscript')) {
            return false;
        }

        // --- Create the "Select All" button ---
        const selectAllButton = document.createElement('button');
        selectAllButton.textContent = 'すべて選択'; // "Select All" in Japanese
        selectAllButton.id = 'selectAllBtn-userscript';
        selectAllButton.type = 'button'; // Prevents form submission
        // Use the same CSS classes as other buttons on the page for a consistent look
        selectAllButton.classList.add('btn', 'btn-info', 'btn-sm', 'ml-3', 'mb-2');

        selectAllButton.addEventListener('click', () => {
            const checkboxes = document.querySelectorAll('.facilities tbody input[type="checkbox"]');
            checkboxes.forEach(checkbox => {
                // If the checkbox is not already checked, click it.
                if (!checkbox.checked) {
                    // We use .click() to ensure any JavaScript frameworks (like Vue.js)
                    // on the page correctly register the change.
                    checkbox.click();
                }
            });
        });

        // --- Create the "Deselect All" button ---
        const deselectAllButton = document.createElement('button');
        deselectAllButton.textContent = 'すべて解除'; // "Deselect All" in Japanese
        deselectAllButton.id = 'deselectAllBtn-userscript';
        deselectAllButton.type = 'button';
        deselectAllButton.classList.add('btn', 'btn-secondary', 'btn-sm', 'ml-2', 'mb-2');

        deselectAllButton.addEventListener('click', () => {
             const checkboxes = document.querySelectorAll('.facilities tbody input[type="checkbox"]');
            checkboxes.forEach(checkbox => {
                // If the checkbox is checked, click it to uncheck.
                if (checkbox.checked) {
                    checkbox.click();
                }
            });
        });

        // --- Add the new buttons to the page ---
        headerContainer.appendChild(selectAllButton);
        headerContainer.appendChild(deselectAllButton);

        return true; // Buttons added successfully
    }

    // Because the page might load content dynamically, we'll check for the target
    // element every 500 milliseconds until it's found.
    const interval = setInterval(() => {
        if (addControlButtons()) {
            clearInterval(interval); // Stop checking once the buttons are added.
        }
    }, 500);
})();