B2主题自带代码高亮功能增强全选+一键复制,支持隐藏内容

JS代码添加

编辑b2/Assets/fontend/single.js,在最下方插入以下代码。:

window.addEventListener('load', () => {
    // 初始化 ClipboardJS 的基础设置
    function setupCodeToolbarAndClipboard() {
        const preElements = document.querySelectorAll('pre');
        
        preElements.forEach((pre, index) => {
            // 检查是否已经被包装
            if (!pre.parentElement.classList.contains('code-toolbar')) {
                // 创建外层容器
                const wrapper = document.createElement('div');
                wrapper.className = 'code-toolbar';
 
                // 创建工具栏
                const toolbar = document.createElement('div');
                toolbar.className = 'toolbar';
 
                // 创建工具栏项目
                const toolbarItem = document.createElement('div');
                toolbarItem.className = 'toolbar-item';
 
                // 创建全选按钮
                const selectAllButton = document.createElement('button');
                selectAllButton.textContent = '全选';
 
                // 创建复制按钮
                const copyButton = document.createElement('button');
                copyButton.textContent = '一键复制';
                copyButton.setAttribute('data-clipboard-target', '#copy' + index);
 
                // 设置 pre 的 ID
                pre.id = 'copy' + index;
 
                // 将 pre 元素包装在新容器中
                pre.parentNode.insertBefore(wrapper, pre);
                wrapper.appendChild(pre);
 
                // 添加按钮到工具栏项目
                toolbarItem.appendChild(selectAllButton);
                toolbarItem.appendChild(document.createElement('span')); // 分隔符
                toolbarItem.appendChild(copyButton);
 
                // 添加工具栏到包装器
                toolbar.appendChild(toolbarItem);
                wrapper.appendChild(toolbar);
 
                // 为全选按钮添加事件监听器
                selectAllButton.addEventListener('click', () => {
                    const range = document.createRange();
                    range.selectNodeContents(pre);
                    const selection = window.getSelection();
                    selection.removeAllRanges();
                    selection.addRange(range);
                });
 
                // 初始化 ClipboardJS
                const clipboardCopy = new ClipboardJS(copyButton);
                
                clipboardCopy.on('success', function(e) {
                    e.clearSelection();
                    const trigger = e.trigger;
                    const toolbarItem = trigger.parentElement;
                    const selectAllButton = toolbarItem.querySelector('button:first-of-type');
 
                    // 禁用所有按钮并显示复制成功消息
                    trigger.textContent = '复制成功';
                    trigger.disabled = true;
                    selectAllButton.disabled = true;
 
                    setTimeout(() => {
                        trigger.textContent = '一键复制';
                        trigger.disabled = false;
                        selectAllButton.disabled = false;
                    }, 2000);
                });
            }
        });
    }
 
    // 使用 MutationObserver 监控 DOM 变化
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                Array.from(mutation.addedNodes).forEach(node => {
                    if (node.nodeName === 'PRE' && !node.parentElement.classList.contains('code-toolbar')) {
                        // 创建一个临时的 preElements 集合,仅包含这个新添加的 pre
                        const newPreElements = document.querySelectorAll('pre:not(.processed)');
                        newPreElements.forEach((pre, index) => {
                            // 标记为已处理,避免重复处理
                            pre.classList.add('processed');
 
                            // 在这里重新调用 setupCodeToolbarAndClipboard 的逻辑,但只针对这个新的 pre
                            // 注意:这里我们其实可以优化,只处理新的这个 pre,而不是重新遍历所有 pre
                            // 但为了简单起见,这里还是重新调用整个函数,并依赖 .processed 类来避免重复处理
                            setupCodeToolbarAndClipboard();
                        });
                    }
                });
            }
        });
    });
 
    // 配置观察选项:
    const config = { childList: true, subtree: true };
 
    // 选择需要观察变动的节点
    const targetNode = document.body;
 
    // 启动观察
    observer.observe(targetNode, config);
 
    // 首次加载时立即处理所有已存在的 pre 元素
    setTimeout(setupCodeToolbarAndClipboard, 600);
});
/**add**/

请注意如果使用了优化JS等插件可能会报错。

CSS代码添加
将以下代码添加进主题的CSS文件中,如果你使用的是B2子主题,那么请将以下CSS添加进子主题目录中的style.css中:

温馨提示:本站提供的一切软件、教程和内容信息都来自网络收集整理,仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,版权争议与本站无关。用户必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解! 联系邮箱:lgg.sinyi@qq.com

给TA打赏
共{{data.count}}人
人已打赏
CMS教程

wordpress教程B2 Pro主题问答模块新增OpenAI ChatGPT机器人自动回答功能

2025-9-16 12:23:10

CMS教程

B2Pro美化教程文章信息头部导航,资源类文章类型美化

2025-9-16 22:44:08

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索