CSOZ Copy Code Button

Add a copy button to code blocks with improved styling

目前为 2024-11-19 提交的版本。查看 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址