玩转CSDN

仅供参考学习

当前为 2021-09-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        玩转CSDN
// @namespace     http://tampermonkey.net/
// @version         0.1008
// @match        *://*.blog.csdn.net/*
// @icon        chrome://favicon/http://blog.csdn.net/
// @description   仅供参考学习
// @author        haiw2
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';
    csdn.copyright.textData = '';
    let login = document.querySelector('#app');
    if(login!=null)document.body.removeChild(login);
    let xx = document.querySelectorAll('a[href*="fn"]')
    xx.forEach((item, index)=>{
        console.info(item.id);
        let node = document.querySelector(`#${item.id}`);
        item.removeAttribute('onclick');
        item.addEventListener('click',function(){
            node.scrollIntoView({ behavior: "smooth" })
        });
    })
    class CSDN{
        construct(focusSwitch){
            console.log('构造方法');
        };

        /*
        @name    复制
        @func
            modifyCopyPriviledge: 修改权限
            copy: 复制逻辑
        */
        modifyCopyPriviledge(codeElem,signElem){
            codeElem.removeAttribute('onclick');
            codeElem.style.setProperty('user-select','auto');
            codeElem.parentNode.style.setProperty('user-select','auto');
            codeElem.parentNode.style.setProperty('margin-bottom','30px');
            try{
                signElem.removeAttribute('onclick');
                signElem.setAttribute('data-title','点击复制');
                signElem.style.margin = '0';
                signElem.style.setProperty('margin-bottom', '40px');
                signElem.style.setProperty('max-width','80px');
                signElem.style.setProperty('min-height','15px');
                signElem.style.setProperty('font-size','15px');
                signElem.style.left = '56px';
                return true
            }catch(e){
                return false
            }finally{
                    codeElem.parentNode.insertBefore(signElem, codeElem);
            }
        };
        copy(signElem) {
            let codeelem = null;
            try{
                codeelem = signElem.parentNode;
                if(codeelem.id.indexOf('code')===-1){
                    throw new EvalError('值错误');
                }
            }catch(err){
                codeelem = signElem.previousElementSibling;
            }
            document.oncopy=function(e){
                e.clipboardData.setData('text',codeelem.innerText);
                e.preventDefault();
                document.oncopy=null;
            }
            document.execCommand("Copy");
            signElem.setAttribute('data-title','复制成功');
            signElem.style.cssText += 'background-color: green';
            setTimeout(()=>{
                signElem.setAttribute('data-title','点击复制');
                signElem.style.removeProperty('background-color','green');
            },1000);
        };
        /*
        @name 专注模式
        @func
            modifyFocusPriviledge: 修改专注权限
            showFocusModel: 展开专注模式
        */
        modifyFocusPriviledge(mainBox, main, catalog){
            let toolbar = document.querySelector('.csdn-side-toolbar');
            toolbar.style.right = "20px";
            toolbar.style.position = "fixed";
            let listen_op = {attributes:true};
            function callback(mutationsList){
                toolbar.style.left = "";
            };
            var observer = new MutationObserver(callback);
            observer.observe(toolbar, listen_op);
            let focusSwitch = document.querySelector('.option-box').cloneNode(true);
            focusSwitch.firstElementChild.src = 'https://s1.aigei.com/src/img/png/05/055f0df239ef4451a25be1e5c4617f96.png?imageMogr2/auto-'+
                'orient/thumbnail/!199x199r/gravity/Center/crop/199x199/quality/85/&e=1735488000&'+
                'token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:pxpJ0L3fOUppABVi15gOFs94eqk=';
            focusSwitch.style.background = "rgba(0,0,0,0.1)";
            focusSwitch.firstElementChild.style.width = "200%";
            focusSwitch.firstElementChild.style.display = "block";
            focusSwitch.removeChild(focusSwitch.lastElementChild);
            focusSwitch.removeAttribute('mouseup');
            toolbar.replaceChildren(focusSwitch);
            toolbar.style.left = '0px';
            main.removeAttribute('id');
            mainBox.firstElementChild.nextElementSibling.style.removeProperty('z-index');
            main.style.cssText += 'z-index:999;position:absolute';
            main.style.display = 'none';
            document.body.insertBefore(main, document.body.firstElementChild);
            if(catalog!=null){
                let pn = catalog.parentNode;
                let _catalog = catalog.cloneNode(true);
                _catalog.style.cssText += 'z-index:999;position:fixed;background-color: rgba(255,255,255,0.2);position: fixed;top: 80px;color: white;right: 20px;';
                _catalog.style.setProperty('max-height','454.5px');
                _catalog.firstElementChild.style.setProperty('max-height','inherit');
                _catalog.firstElementChild.firstElementChild.style.cssText += 'text-align:center;background:black;color:white';
                _catalog.style.display = 'none';
                _catalog.removeAttribute('id');
                let className = (function(){
                    let arr = [];
                    for(let i=0;i<3;i++){
                        if(pn.className.length){
                            arr.push(pn.className);
                        }
                        pn = pn.parentNode;
                    }
                    return arr.join(' ')
                })();
                _catalog.className += ' ' + className;
                document.body.insertBefore(_catalog, document.body.firstElementChild);
                return {a: focusSwitch,b: _catalog};
            }
            return {a:focusSwitch,b:catalog}

        };
        showFocusModel(status, mainBox, main,catalog){
            if(status===0){
                mainBox.style.display = 'none';
                mainBox.nextElementSibling.style.display = 'none';
                main.style.display = 'block';
                if(catalog!=null){
                    catalog.style.display = 'block';
                    catalog.setAttribute('id','groupfile');
                }
            }else{
                mainBox.style.display = 'block';
                mainBox.nextElementSibling.style.display = 'block';
                main.style.display = 'none';
                if(catalog!=null){
                    catalog.style.display = 'none';
                    catalog.removeAttribute('id');
                }
            }
        }
    };

    let _csdn = new CSDN();
    let mainBox = document.getElementById('mainBox');
    let main = mainBox.firstElementChild.cloneNode(true);
    let catalog = document.getElementById('groupfile');
    let compose = _csdn.modifyFocusPriviledge(mainBox, main, catalog);
    let status = -1;
    compose.a.addEventListener('click', function(){
        status = ~status;
        _csdn.showFocusModel(status,mainBox,main,compose.b);
    },'true');
    let codes = document.querySelectorAll('#content_views pre code');
    let signs = document.querySelectorAll('#content_views .signin');
    if(signs.length===0) signs = document.querySelectorAll('#content_views .hljs-button');
    for(var i=0;i<codes.length;i++){
        let sign = signs[i], code = codes[i];
        _csdn.modifyCopyPriviledge(code, sign);
        sign.addEventListener('click', function(){
            GM_setClipboard(code.innerText, "text");
            sign.setAttribute('data-title','复制成功');
            sign.style.cssText += 'background-color: green';
            setTimeout(()=>{
                sign.setAttribute('data-title','点击复制');
                sign.style.removeProperty('background-color','green');
            },1000);
            // _csdn.copy(this);
        },'true');
    };
})();