CSDN 一键复制

一键复制CSDN代码块

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         CSDN 一键复制
// @namespace    http://tampermonkey.net/
// @version      1.0.3
// @description  一键复制CSDN代码块
// @author       Hz
// @match        blog.csdn.net/**
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var codes = document.getElementsByTagName('pre');
    var timer = null;

    for(var i=0;i<codes.length;i++) {
        var block = codes[i];
        var btn = document.createElement('div');
        btn.style.position = 'absolute';
        btn.style.right = '100px';
        btn.style.top = '4px';
        btn.style.fontSize = '12px';
        btn.style.color = '#4d4d4d';
        btn.style.backgroundColor = 'white';
        btn.style.padding = '2px 8px';
        btn.style.margin = '8px';
        btn.style.borderRadius = '4px';
        btn.style.cursor = 'pointer';
        btn.style.boxShadow = '0 2px 4px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.05)';
        btn.innerText = '一键复制';
        block.appendChild(btn);
        btn.onclick = (function(index){
            return function(){
                copyCode(index)
            }
        })(i)
    }

    function copyCode(blockIndex) {
        var str = codes[blockIndex].children[0].innerText;
        var result = false;
        var saveString = function(e){
            e.clipboardData.setData('text/plain', str);
            e.preventDefault();
        }
        document.addEventListener('copy', saveString);
        result = document.execCommand('copy');
        document.removeEventListener('copy', saveString);
        var btn = codes[blockIndex].children[codes[blockIndex].children.length - 1];
        btn.innerText = '已复制';
        if(timer) {
            clearTimeout(timer);
            timer = null;
        }
        timer = setTimeout(function(){
            btn.innerText = '一键复制';
            clearTimeout(timer);
            timer = null;
        }, 3000);
        return result;
    }


    var masks = document.querySelectorAll('.hide-article-box')
    for(var m=0;m<masks.length;m++){
        masks[m].parentElement.removeChild(masks[m]);
    }
    var articleContent = document.getElementById('article_content');
    if(articleContent) {
        articleContent.style.height = 'auto';
    }
})();