Github Repository Removal Simplifier

Simplify github repository deleting

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Github Repository Removal Simplifier
// @namespace    https://github.com/quickyyy
// @version      1.4
// @description  Simplify github repository deleting
// @author       quickyyy
// @match        https://github.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant        none
// @license      GNU GPLv3 
// ==/UserScript==

(function() {
    'use strict';

    function handleModal(modal) {
        const inputField = modal.querySelector('input[id="verification_field"]');
        
        if (!inputField) return;

        if (inputField.dataset.processed) return;
        inputField.dataset.processed = "true";

        let expectedText = inputField.getAttribute('data-repo-nwo');

        if (!expectedText) {
            const label = modal.querySelector('label[for="verification_field"]');
            if (label) {
                const match = label.textContent.match(/type "(.*?)"/);
                if (match && match[1]) {
                    expectedText = match[1];
                }
            }
        }

        if (!expectedText) return;

        const container = document.createElement('div');
        container.style.marginTop = '10px';
        container.style.marginBottom = '10px';
        container.style.display = 'flex';
        container.style.alignItems = 'center';
        container.style.justifyContent = 'flex-start'; 

        const checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.id = 'tm-delete-confirm-checkbox';
        checkbox.style.marginRight = '8px';
        checkbox.style.cursor = 'pointer';
        checkbox.style.width = 'auto'; 

        const label = document.createElement('label');
        label.htmlFor = 'tm-delete-confirm-checkbox';
        label.textContent = `I confirm I want to delete ${expectedText}`;
        label.style.cursor = 'pointer';
        label.style.userSelect = 'none';
        label.style.fontWeight = 'bold';

        container.appendChild(checkbox);
        container.appendChild(label);

        const containerToHide = inputField.closest('primer-text-field') || inputField.closest('.FormControl') || inputField.parentNode;

        containerToHide.parentNode.insertBefore(container, containerToHide);

        containerToHide.style.opacity = '0';
        containerToHide.style.position = 'absolute';
        containerToHide.style.pointerEvents = 'none';
        containerToHide.style.height = '0';
        containerToHide.style.overflow = 'hidden';

        checkbox.addEventListener('change', function() {
            if (this.checked) {
                inputField.value = expectedText;
                inputField.setAttribute('value', expectedText);
            } else {
                inputField.value = '';
                inputField.setAttribute('value', '');
            }
            
            const events = ['focus', 'input', 'change', 'keyup', 'blur'];
            events.forEach(eventType => {
                const event = new Event(eventType, { bubbles: true });
                inputField.dispatchEvent(event);
            });
        });
    }

    function scanForModals() {
        const inputs = document.querySelectorAll('input[id="verification_field"]');
        inputs.forEach(input => {
             const modal = input.closest('dialog') || input.closest('details-dialog') || input.closest('div[role="dialog"]') || input.closest('form');
             if (modal) handleModal(modal);
        });
    }

    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            for (const node of mutation.addedNodes) {
                if (node.nodeType === 1) { 
                    if (node.tagName === 'DIALOG' || node.matches('details-dialog') || node.querySelector('input[id="verification_field"]')) {
                         const target = (node.tagName === 'DIALOG' || node.matches('details-dialog')) ? node : node.querySelector('input[id="verification_field"]').closest('dialog, details-dialog, form');
                         if (target) handleModal(target);
                    }
                    
                    const input = node.querySelector('input[id="verification_field"]');
                    if (input) {
                        const modal = input.closest('dialog') || input.closest('details-dialog') || input.closest('div[role="dialog"]') || input.closest('form');
                        if (modal) handleModal(modal);
                    }
                }
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

    scanForModals();

    document.addEventListener('turbo:load', scanForModals);
    document.addEventListener('pjax:end', scanForModals);

})();