Codeshare Auto Paste

Automatically paste clipboard content into codeshare.io/new on load.

2026-02-23 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Codeshare Auto Paste
// @namespace    fiverr.com/web_coder_nsd
// @version      1.0
// @description  Automatically paste clipboard content into codeshare.io/new on load.
// @author       NoushadBug
// @match        https://codeshare.io/*
// @icon         https://codeshare.io/favicon.ico
// @grant        none
// @license      MIT
// ==/UserScript==

(async function () {
    'use strict';

    function getCodeMirrorInstance() {
        // CodeMirror attaches itself to the wrapper div
        const cmEl = document.querySelector('.CodeMirror');
        return cmEl && cmEl.CodeMirror ? cmEl.CodeMirror : null;
    }

    function waitForCodeMirror(timeout = 10000) {
        return new Promise((resolve, reject) => {
            const start = Date.now();
            const interval = setInterval(() => {
                const cm = getCodeMirrorInstance();
                if (cm) {
                    clearInterval(interval);
                    resolve(cm);
                } else if (Date.now() - start > timeout) {
                    clearInterval(interval);
                    reject(new Error('CodeMirror instance not found within timeout.'));
                }
            }, 200);
        });
    }

    async function handleClipboard() {
        try {
            const permissionStatus = await navigator.permissions.query({ name: 'clipboard-read' });
            if (permissionStatus.state === 'denied') {
                alert('Clipboard read access is required. Please allow it in your browser settings.');
                return;
            }

            const clipboardText = await navigator.clipboard.readText();
            console.log('Clipboard content retrieved:', clipboardText);

            const cm = await waitForCodeMirror();

            // Use CodeMirror API to set value — this integrates properly with Firepad
            cm.setValue(clipboardText);

            // Move cursor to end
            cm.setCursor(cm.lineCount(), 0);

            console.log('Pasted clipboard content into CodeMirror/Firepad editor.');
        } catch (error) {
            console.error('Error handling clipboard or interacting with the editor:', error);
        }
    }

    window.addEventListener('load', async () => {
        // Give Firepad extra time to initialize after page load
        setTimeout(async () => {
            try {
                await handleClipboard();
            } catch (error) {
                console.error('An error occurred while attempting to process the clipboard:', error);
            }
        }, 2000);
    });
})();