// ==UserScript== // @name Gofile.io Dual Copy Buttons for Code Blocks (Mobile Friendly, Orange Feedback) // @namespace http://tampermonkey.net/ // @version 1.1 // @description Adds both a vertical and a bottom "Copy" button to each code block on Gofile.io, with mobile-friendly clipboard support and orange feedback. // @author nn // @match https://gofile.io/* // @icon https://gofile.io/favicon.ico // @grant none // ==/UserScript== (function() { 'use strict'; // Observe DOM for dynamically loaded content const observer = new MutationObserver(() => addCopyButtons()); observer.observe(document.body, { childList: true, subtree: true }); // Mobile-friendly copy function async function mobileFriendlyCopy(text) { // Try Clipboard API first if (navigator.clipboard && navigator.clipboard.writeText) { try { await navigator.clipboard.writeText(text); return true; } catch (err) { // Fallback below } } // Fallback: create a temporary textarea and select/copy try { const textarea = document.createElement('textarea'); textarea.value = text; // Avoid scrolling to bottom on iOS textarea.style.position = 'fixed'; textarea.style.top = 0; textarea.style.left = 0; textarea.style.width = '2em'; textarea.style.height = '2em'; textarea.style.padding = 0; textarea.style.border = 'none'; textarea.style.outline = 'none'; textarea.style.boxShadow = 'none'; textarea.style.background = 'transparent'; document.body.appendChild(textarea); textarea.focus(); textarea.select(); const successful = document.execCommand('copy'); document.body.removeChild(textarea); return successful; } catch (err) { return false; } } function getCodeText(pre) { // Prefer child if present const codeElem = pre.querySelector('code'); if (codeElem) { return codeElem.textContent; } else { // Clone
, remove button(s), get text
            const clone = pre.cloneNode(true);
            // Remove all copy buttons (vertical and bottom)
            clone.querySelectorAll('.tm-vertical-copy-btn, .tm-second-copy-btn').forEach(btn => btn.remove());
            return clone.textContent;
        }
    }

    function addCopyButtons() {
        document.querySelectorAll('pre.max-h-screen.max-w-full.language-text').forEach(pre => {
            // Ensure the parent is positioned relatively for absolute child (vertical button)
            pre.style.position = 'relative';

            // --- Vertical Copy Button (right edge) ---
            if (!pre.querySelector('.tm-vertical-copy-btn')) {
                const vertBtn = document.createElement('button');
                vertBtn.textContent = 'Copy';
                vertBtn.className = 'tm-vertical-copy-btn';
                vertBtn.style.cssText = `
                    position: absolute;
                    top: 0;
                    right: 0;
                    height: 100%;
                    width: 56px;
                    min-width: 48px;
                    background: #007aff;
                    color: #fff;
                    border: none;
                    border-radius: 0 4px 4px 0;
                    cursor: pointer;
                    font-size: 18px;
                    writing-mode: vertical-rl;
                    text-orientation: mixed;
                    z-index: 10;
                    transition: background 0.2s;
                    box-shadow: -2px 0 8px rgba(0,0,0,0.06);
                    touch-action: manipulation;
                    user-select: none;
                `;
                vertBtn.addEventListener('click', async () => {
                    const codeText = getCodeText(pre);
                    const ok = await mobileFriendlyCopy(codeText);
                    if (ok) {
                        vertBtn.textContent = 'Copied!';
                        vertBtn.style.background = 'orange';
                        setTimeout(() => {
                            vertBtn.textContent = 'Copy';
                            vertBtn.style.background = '#007aff';
                        }, 1200);
                    } else {
                        vertBtn.textContent = 'Failed!';
                        vertBtn.style.background = 'red';
                        setTimeout(() => {
                            vertBtn.textContent = 'Copy';
                            vertBtn.style.background = '#007aff';
                        }, 1200);
                    }
                });
                pre.appendChild(vertBtn);
            }

            // --- Bottom Copy Button (horizontal, centered) ---
            if (!pre.querySelector('.tm-second-copy-btn')) {
                const bottomBtn = document.createElement('button');
                bottomBtn.textContent = 'Copy';
                bottomBtn.className = 'tm-second-copy-btn';
                bottomBtn.style.cssText = `
                    display: block;
                    margin: 12px auto 0 auto;
                    padding: 12px 24px;
                    background: #007aff;
                    color: #fff;
                    border: none;
                    border-radius: 8px;
                    cursor: pointer;
                    font-size: 18px;
                    min-width: 96px;
                    touch-action: manipulation;
                    user-select: none;
                `;
                bottomBtn.addEventListener('click', async () => {
                    const codeText = getCodeText(pre);
                    const ok = await mobileFriendlyCopy(codeText);
                    if (ok) {
                        bottomBtn.textContent = 'Copied!';
                        bottomBtn.style.background = 'orange';
                        setTimeout(() => {
                            bottomBtn.textContent = 'Copy';
                            bottomBtn.style.background = '#007aff';
                        }, 1200);
                    } else {
                        bottomBtn.textContent = 'Failed!';
                        bottomBtn.style.background = 'red';
                        setTimeout(() => {
                            bottomBtn.textContent = 'Copy';
                            bottomBtn.style.background = '#007aff';
                        }, 1200);
                    }
                });
                pre.appendChild(bottomBtn);
            }
        });
    }

    // Initial run
    addCopyButtons();
})();