Stackoverflow Copier

Adds buttons to copy stackoverflow's codeblock content.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Stackoverflow Copier
// @namespace    https://letga.me
// @version      0.2
// @description  Adds buttons to copy stackoverflow's codeblock content.
// @author       LetGame
// @license      MIT
// @match        https://stackoverflow.com/questions/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=stackoverflow.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    window.addEventListener('load', function() {
        var codeblocks = document.querySelectorAll('pre.s-code-block');
        for (var i = 0; i < codeblocks.length; i++) {
            try {
                var copybutton = document.createElement('input');

                // Adding most important attributes
                copybutton.type = 'button';
                copybutton.value = 'Copy!';
                copybutton.setAttribute('onclick', 'navigator.clipboard.writeText(this.parentElement.innerText); setTimeout(() => {alert("Copied!")}, 100)');

                // Styling the button
                var s = copybutton.style; // Shortcut to not write copybutton.style over and over again
                codeblocks[i].setAttribute('style', 'position: relative;');

                s.position = 'absolute';
                s.top = '0.5em';
                s.right = '0.5em';
                s.margin = '0';
                s.cursor = 'pointer';
                copybutton.classList.add('post-tag');

                // Appending the button to codeblock
                codeblocks[i].appendChild(copybutton);
                console.log('Successfully added a copy function to codeblock ' + i + '.');
            } catch(e) {console.error('Adding a copy function to codeblock ' + i + ' failed. (' + e + ')');}
        }
    }, false);
})();