CSDN 一键复制

一键复制CSDN代码块

目前為 2019-04-04 提交的版本,檢視 最新版本

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

(function() {
    'use strict';
    var codes = document.getElementsByClassName('prettyprint');
    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;
    }


    // Your code here...
})();