GitHub Gist Copier

Adds copy button to Gist files for easy code copying.

目前為 2025-04-07 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GitHub Gist Copier
// @description  Adds copy button to Gist files for easy code copying.
// @icon         https://github.githubassets.com/favicons/favicon-dark.svg
// @version      1.1
// @author       afkarxyz
// @namespace    https://github.com/afkarxyz/userscripts/
// @supportURL   https://github.com/afkarxyz/userscripts/issues
// @license      MIT
// @run-at       document-end
// @match        https://gist.github.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @connect      githubusercontent.com
// ==/UserScript==

(function() {
    'use strict';

    function noop() { }

    function debounce(f, delay) {
        let timeoutId = null;
        return function (...args) {
            if (timeoutId) {
                clearTimeout(timeoutId);
            }
            timeoutId = setTimeout(() => {
                f.apply(this, args);
            }, delay);
        };
    }

    function createCopyButton(fileElement) {
        const fileActionElement = fileElement.querySelector('.file-actions');
        if (!fileActionElement) {
            return noop;
        }

        const rawButton = fileActionElement.querySelector('a[href*="/raw/"]');
        if (!rawButton) {
            return noop;
        }
        
        const button = document.createElement('button');
        button.className = 'btn-octicon gist-copy-button';
        button.style.marginRight = '5px';

        button.innerHTML = `
        <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy">
            <path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path>
        </svg>

        <svg style="display: none;" aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-check color-fg-success">
            <path d="M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z"></path>
        </svg>
        `;

        const copyIcon = button.querySelector('.octicon-copy');
        const checkIcon = button.querySelector('.octicon-check');

        let timeoutId = null;
        const copyHandler = (e) => {
            if (timeoutId) {
                return;
            }
            e.preventDefault();

            const rawUrl = rawButton.href;
            
            GM_xmlhttpRequest({
                method: 'GET',
                url: rawUrl,
                onload: function(response) {
                    if (response.status === 200) {
                        GM_setClipboard(response.responseText, { type: 'text', mimetype: 'text/plain' });
                        
                        copyIcon.style.display = 'none';
                        checkIcon.style.display = 'inline-block';
                        
                        timeoutId = setTimeout(() => {
                            copyIcon.style.display = 'inline-block';
                            checkIcon.style.display = 'none';
                            timeoutId = null;
                        }, 500);
                    }
                },
                onerror: function(error) {
                    timeoutId = null;
                }
            });
        };

        button.addEventListener('click', copyHandler);
        fileActionElement.insertBefore(button, fileActionElement.firstChild);

        return () => {
            button.removeEventListener('click', copyHandler);
            if (timeoutId) {
                clearTimeout(timeoutId);
            }
            button.remove();
        };
    }

    function runGistCopy() {
        let removeAllListeners = noop;
        
        function tryCreateCopyButtons() {
            removeAllListeners();
            const fileElements = [...document.querySelectorAll('.file')];
            const removeListeners = fileElements.map(createCopyButton);
            removeAllListeners = () => {
                removeListeners.map((f) => f());
                [...document.querySelectorAll('.gist-copy-button')].forEach((el) => {
                    el.remove();
                });
            };
        }

        setTimeout(tryCreateCopyButtons, 300);

        const observer = new MutationObserver(debounce(() => {
            if (document.querySelectorAll('.file').length > 0 &&
                document.querySelectorAll('.gist-copy-button').length === 0) {
                tryCreateCopyButtons();
            }
        }, 100));

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
        
        if (window.onurlchange === null) {
            window.addEventListener('urlchange', debounce(tryCreateCopyButtons, 16));
        }
    }

    runGistCopy();
})();