Stackoverflow Add Button For Copy to clipboard

Stackoverflow Add Button For Copy to clipboard.

Stan na 05-01-2022. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==

// @namespace    https://openuserjs.org/users/a29527806
// @name         Stackoverflow Add Button For Copy to clipboard
// @description  Stackoverflow Add Button For Copy to clipboard.
// @copyright    2022, HrJasn (https://openuserjs.org/users/a29527806)
// @license      GPL-3.0-or-later
// @version      1.2
// @icon         https://www.google.com/s2/favicons?domain=stackoverflow.com
// @include      https://stackoverflow.com/questions/*
// @grant        none
// ==/UserScript==

// ==OpenUserJS==
// @author       a29527806
// ==/OpenUserJS==

window.onload = function() {

    const precodeObjs = document.querySelectorAll('pre code');
    
    const scrollbarChange = function(){
        let copyBtn = this.querySelector('input[value="Copy"]');
        copyBtn.style.right = (5-this.scrollLeft) + 'px';
        copyBtn.style.bottom = (5-this.scrollTop) + 'px';
    }

    precodeObjs.forEach(function(precodeObj){
        let copyBtn = document.createElement('input');
        copyBtn.type='button';
        copyBtn.value='Copy';
        copyBtn.style='cursor:pointer;opacity:0;position:absolute;right:5px;bottom:5px;font-size:15px;padding:10px;background-color:#888888;border:0px;border-style:solid;color: #ffffff;text-shadow: 0px 0px 2px #ffffff;border-radius:5px;';
        precodeObj.parentNode.style.position = 'relative';
        precodeObj.parentNode.appendChild(copyBtn);
        precodeObj.parentNode.addEventListener("wheel",scrollbarChange,false);
        precodeObj.parentNode.addEventListener("scroll",scrollbarChange,false);
        precodeObj.parentNode.addEventListener("keydown",scrollbarChange,false);
        copyBtn.addEventListener('click', function(){
            let targetObj = this.parentNode;
            let srcText = targetObj.textContent || targetObj.innerText || targetObj.nodeValue || '';
            navigator.clipboard.writeText(srcText);
            //let clipText = navigator.clipboard.readText();
            console.log(srcText);
            if(srcText !== undefined){
                this.value='Copied';
            }
        });
        copyBtn.addEventListener('mouseenter', function(){
            this.style.opacity = 1;
        });
        copyBtn.addEventListener('mouseleave', function(){
            this.style.opacity = 0;
        });
    });

}