CSOZ Copy Code Button

Add a copy button to code blocks with improved styling

19.11.2024 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         CSOZ Copy Code Button
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add a copy button to code blocks with improved styling
// @author       Y.V
// @license      AGPL-3.0-or-later
// @match        https://oj.czos.cn/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 等待页面加载完成
    window.addEventListener('load', function() {
        // 查找所有代码块
        const codeBlocks = document.querySelectorAll('div.markdown pre code');

        codeBlocks.forEach(codeBlock => {
            // 创建复制按钮
            const copyButton = document.createElement('button');
            copyButton.innerText = '复制';
            copyButton.style.marginLeft = '10px';
            copyButton.style.backgroundColor = '#007bff';
            copyButton.style.color = 'white';
            copyButton.style.border = 'none';
            copyButton.style.padding = '5px 10px';
            copyButton.style.cursor = 'pointer';
            copyButton.style.borderRadius = '5px';
            copyButton.style.fontSize = '14px';
            copyButton.style.transition = 'background-color 0.3s ease';

            // 将按钮插入到代码块旁边
            codeBlock.parentNode.insertBefore(copyButton, codeBlock.nextSibling);

            // 添加点击事件
            copyButton.addEventListener('click', function() {
                const codeText = codeBlock.innerText;
                navigator.clipboard.writeText(codeText).then(function() {
                    alert('代码已复制到剪贴板');
                }).catch(function(err) {
                    console.error('复制失败: ', err);
                });
            });

            // 添加悬停效果
            copyButton.addEventListener('mouseover', function() {
                copyButton.style.backgroundColor = '#0056b3';
            });

            copyButton.addEventListener('mouseout', function() {
                copyButton.style.backgroundColor = '#007bff';
            });
        });
    });
})();