CSOZ Copy Code Button

Add a copy button to code blocks with improved styling

اعتبارا من 19-11-2024. شاهد أحدث إصدار.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==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';
            });
        });
    });
})();