Greasyfork/Sleazyfork Code Copier

Adds a copy button to Greasyfork/Sleazyfork code pages.

Tính đến 02-12-2024. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Greasyfork/Sleazyfork Code Copier
// @description  Adds a copy button to Greasyfork/Sleazyfork code pages.
// @icon         https://greasyfork.org/vite/assets/blacklogo96-CxYTSM_T.png
// @version      1.1
// @author       afkarxyz
// @namespace    https://github.com/afkarxyz/misc-scripts/
// @supportURL   https://github.com/afkarxyz/misc-scripts/issues
// @license      MIT
// @match        https://greasyfork.org/*/scripts/*/code
// @match        https://greasyfork.org/*/scripts/*/code?version=*
// @match        https://sleazyfork.org/*/scripts/*/code
// @match        https://sleazyfork.org/*/scripts/*/code?version=*
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    function showGreasyforkScript() {
        const codeLines = Array.from(
            document.querySelectorAll('.linenums li'),
            line => line.textContent
        );
        console.log(codeLines.join('\n'));
        return codeLines.join('\n');
    }

    const copyToClipboard = text => {
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.style.position = 'fixed';
        textarea.style.opacity = '0';
        document.body.appendChild(textarea);
        textarea.select();
        document.execCommand('copy');
        document.body.removeChild(textarea);
    };

    const createSVGElement = (pathD) => {
        const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
        const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
        
        svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
        svg.setAttribute('viewBox', '0 0 384 512');
        svg.setAttribute('width', '14');
        svg.setAttribute('height', '16');
        
        path.setAttribute('fill', 'currentColor');
        path.setAttribute('d', pathD);
        
        svg.appendChild(path);
        return svg;
    };

    const addIconButton = () => {
        const installArea = document.getElementById('install-area');
        if (!installArea || document.querySelector('.custom-icon-link')) return;

        const initialIconPath = 'M145.5 68c5.3-20.7 24.1-36 46.5-36s41.2 15.3 46.5 36c1.8 7.1 8.2 12 15.5 12l18 0c8.8 0 16 7.2 16 16l0 32-96 0-96 0 0-32c0-8.8 7.2-16 16-16l18 0c7.3 0 13.7-4.9 15.5-12zM192 0c-32.8 0-61 19.8-73.3 48L112 48C91.1 48 73.3 61.4 66.7 80L64 80C28.7 80 0 108.7 0 144L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-304c0-35.3-28.7-64-64-64l-2.7 0c-6.6-18.6-24.4-32-45.3-32l-6.7 0C253 19.8 224.8 0 192 0zM320 112c17.7 0 32 14.3 32 32l0 304c0 17.7-14.3 32-32 32L64 480c-17.7 0-32-14.3-32-32l0-304c0-17.7 14.3-32 32-32l0 16c0 17.7 14.3 32 32 32l96 0 96 0c17.7 0 32-14.3 32-32l0-16zM208 80a16 16 0 1 0 -32 0 16 16 0 1 0 32 0zM136 272a24 24 0 1 0 -48 0 24 24 0 1 0 48 0zm40-16c-8.8 0-16 7.2-16 16s7.2 16 16 16l96 0c8.8 0 16-7.2 16-16s-7.2-16-16-16l-96 0zm0 96c-8.8 0-16 7.2-16 16s7.2 16 16 16l96 0c8.8 0 16-7.2 16-16s-7.2-16-16-16l-96 0zm-64 40a24 24 0 1 0 0-48 24 24 0 1 0 0 48z';
        const alternateIconPath = 'M145.5 68c5.3-20.7 24.1-36 46.5-36s41.2 15.3 46.5 36c1.8 7.1 8.2 12 15.5 12l18 0c8.8 0 16 7.2 16 16l0 32-96 0-96 0 0-32c0-8.8 7.2-16 16-16l18 0c7.3 0 13.7-4.9 15.5-12zM192 0c-32.8 0-61 19.8-73.3 48L112 48C91.1 48 73.3 61.4 66.7 80L64 80C28.7 80 0 108.7 0 144L0 448c0 35.3 28.7 64 64 64l256 0c35.3 0 64-28.7 64-64l0-304c0-35.3-28.7-64-64-64l-2.7 0c-6.6-18.6-24.4-32-45.3-32l-6.7 0C253 19.8 224.8 0 192 0zM320 112c17.7 0 32 14.3 32 32l0 304c0 17.7-14.3 32-32 32L64 480c-17.7 0-32-14.3-32-32l0-304c0-17.7 14.3-32 32-32l0 16c0 17.7 14.3 32 32 32l96 0 96 0c17.7 0 32-14.3 32-32l0-16zM208 80a16 16 0 1 0 -32 0 16 16 0 1 0 32 0zm91.3 171.3c6.2-6.2 6.2-16.4 0-22.6s-16.4-6.2-22.6 0L160 345.4l-52.7-52.7c-6.2-6.2-16.4-6.2-22.6 0s-6.2 16.4 0 22.6l64 64c6.2 6.2 16.4 6.2 22.6 0l128-128z';

        const iconButton = document.createElement('a');
        iconButton.className = 'install-link custom-icon-link';
        iconButton.style.marginLeft = '0.5rem';
        iconButton.style.borderRadius = '.25rem';
        iconButton.style.cursor = 'pointer';
        iconButton.href = 'javascript:void(0)';

        const initialSvg = createSVGElement(initialIconPath);
        initialSvg.addEventListener('click', (e) => {
            e.preventDefault();
            e.stopPropagation();
            handleCopy();
        });

        iconButton.appendChild(initialSvg);

        const handleCopy = () => {
            const scriptContent = showGreasyforkScript();
            copyToClipboard(scriptContent);
            
            const currentSvg = iconButton.firstChild;
            const newSvg = createSVGElement(alternateIconPath);
            newSvg.addEventListener('click', (e) => {
                e.preventDefault();
                e.stopPropagation();
                handleCopy();
            });
            
            iconButton.replaceChild(newSvg, currentSvg);
            
            setTimeout(() => {
                const revertSvg = createSVGElement(initialIconPath);
                revertSvg.addEventListener('click', (e) => {
                    e.preventDefault();
                    e.stopPropagation();
                    handleCopy();
                });
                iconButton.replaceChild(revertSvg, newSvg);
            }, 500);
        };

        iconButton.onclick = (e) => {
            e.preventDefault();
            e.stopPropagation();
            handleCopy();
        };

        installArea.appendChild(iconButton);
    };

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', addIconButton);
    } else {
        addIconButton();
    }
})();