GitHub Delete Repositories

Add delete button to GitHub repositories

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GitHub Delete Repositories
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add delete button to GitHub repositories
// @author       ChatGPT
// @match        https://github.com/*&tab=repositories*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @connect      api.github.com
// @license      ChatGPT
// ==/UserScript==

(function() {
    'use strict';

    const TOKEN_KEY = 'github_token';

    // Function to get GitHub token from localStorage or prompt user
    function getToken() {
        let token = localStorage.getItem(TOKEN_KEY);
        if (!token) {
            token = prompt("Please enter your GitHub token:");
            if (token) {
                localStorage.setItem(TOKEN_KEY, token);
            } else {
                alert("GitHub token is required to delete repositories.");
            }
        }
        return token;
    }

    // Retrieve the GitHub token
    const GITHUB_TOKEN = getToken();
    if (!GITHUB_TOKEN) return;

    // Add custom styles for the delete button
    GM_addStyle(`
        .delete-repo-btn {
            margin-left: 10px;
            color: white;
            background-color: red;
            border: none;
            padding: 5px 10px;
            cursor: pointer;
        }
    `);

    // Add delete button to each repository item
    const repoItems = document.querySelectorAll('li[itemprop="owns"]');
    repoItems.forEach(item => {
        const repoName = item.querySelector('a[itemprop="name codeRepository"]').textContent.trim();
        const ownerName = window.location.pathname.split('/')[1];
        const deleteButton = document.createElement('button');
        deleteButton.textContent = 'Delete';
        deleteButton.classList.add('delete-repo-btn');
        deleteButton.onclick = function() {
            if (confirm(`Are you sure you want to delete the repository "${repoName}"?`)) {
                deleteRepository(ownerName, repoName);
            }
        };
        item.appendChild(deleteButton);
    });

    // Function to delete repository using GitHub API
    function deleteRepository(owner, repo) {
        GM_xmlhttpRequest({
            method: 'DELETE',
            url: `https://api.github.com/repos/${owner}/${repo}`,
            headers: {
                'Authorization': `token ${GITHUB_TOKEN}`,
                'Accept': 'application/vnd.github.v3+json'
            },
            onload: function(response) {
                if (response.status === 204) {
                    alert(`Repository "${repo}" deleted successfully.`);
                    location.reload();
                } else {
                    alert(`Failed to delete repository "${repo}". Status: ${response.status}`);
                }
            },
            onerror: function(error) {
                alert(`An error occurred: ${error}`);
            }
        });
    }
})();