Stack Overflow Code Copy

Adds a copy button to code blocks on Stack Overflow

Verze ze dne 14. 04. 2024. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Stack Overflow Code Copy
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a copy button to code blocks on Stack Overflow
// @match        https://stackoverflow.com/questions/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function addCopyButton(codeBlock) {
        const button = document.createElement('button');
        button.innerHTML = 'Copy';
        button.style.position = 'absolute';
        button.style.top = '5px';
        button.style.right = '5px';
        button.onclick = function() {
            const code = codeBlock.querySelector('code').innerText;
            navigator.clipboard.writeText(code);
        };
        codeBlock.style.position = 'relative';
        codeBlock.appendChild(button);
    }

    const codeBlocks = document.querySelectorAll('pre');
    codeBlocks.forEach(addCopyButton);
})();