CSOZ Copy Code Button

Add a copy button to code blocks with improved styling

Από την 19/11/2024. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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         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';
            });
        });
    });
})();