玩转CSDN

仅供参考学习

Stan na 08-09-2021. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        玩转CSDN
// @namespace     http://tampermonkey.net/
// @version         0.1006
// @match        *://*.blog.csdn.net/*
// @icon        https://source.unsplash.com/random
// @description   仅供参考学习
// @author        w2hi
// @grant        none
// @reference     https://source.unsplash.com/random
// ==/UserScript==

(function() {
    'use strict';
    class CSDN{
        construct(focusSwitch){
            console.log('构造方法');
        };

        /*
        @name    复制
        @func
            modifyCopyPriviledge: 修改权限
            copy: 复制逻辑
        */
        modifyCopyPriviledge(codeElem,signElem){
            //代码容器修改
            codeElem.removeAttribute('onclick');
            codeElem.style.setProperty('user-select','auto');
            codeElem.setAttribute('id','code-'+i);
            codeElem.parentNode.style.setProperty('user-select','auto');
            // 登录容器修改
            try{
                signElem.removeAttribute('onclick');
                signElem.setAttribute('data-title','点击复制');
                signElem.removeAttribute('data-report-click');
                signElem.setAttribute('data-clipboard-action','copy');
                signElem.setAttribute('aria-label','copied!');
                signElem.setAttribute('data-clipboard-target','#code-'+i);
                signElem.style.position = 'inherit';
                signElem.style.display = "block";
                signElem.style.setProperty('max-width','65px');
                return true
            }catch(e){
                return false
            }
        };
        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(()=>{ // 先延时1000ms,再执行回调函数
                signElem.setAttribute('data-title','点击复制');
                signElem.style.removeProperty('background-color','green');
            },1000);
        };
        /*
        @name 专注模式
        @func
            modifyFocusPriviledge: 修改专注权限
            showFocusModel: 展开专注模式
        */
        modifyFocusPriviledge(mainBox, main, catalog){
            // 修改toolbar
            let toolbar = document.querySelector('.csdn-side-toolbar');
            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);
            toolbar.replaceChildren(focusSwitch);
            // 修改默认页面配置样式
            main.removeAttribute('id');
            focusSwitch.removeAttribute('mouseup');
            mainBox.firstElementChild.nextElementSibling.style.removeProperty('z-index');
            main.style.cssText += 'z-index:999;position:absolute';
            main.style.display = 'none';
            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;
            console.log(_catalog.className);
            document.body.insertBefore(_catalog, document.body.firstElementChild);
            document.body.insertBefore(main, document.body.firstElementChild);
            return {
                a: focusSwitch,
                b: _catalog
            };

        };
        showFocusModel(status, mainBox, main,catalog){
            if(status===0){ // 进入专注模式
                console.log(`----${status}: 专注模式----`)
                mainBox.style.display = 'none';
                mainBox.nextElementSibling.style.display = 'none';
                main.style.display = 'block';
                catalog.style.display = 'block';
                catalog.setAttribute('id','groupfile');
                console.log(catalog.parentNode);
                console.log('主体可见\n全体不可见')
            }else{
                console.log(`----${status}: 初始模式----`);
                mainBox.style.display = 'block';
                mainBox.nextElementSibling.style.display = 'block';
                main.style.display = 'none';
                catalog.style.display = 'none';
                catalog.removeAttribute('id');
                console.log(catalog.parentNode);
                console.log('主体不可见\n全体可见');
            }
        }
    };

    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('.signin');
    // 解决部分文章允许不登陆复制的脚本定位问题
    if(signs.length===0) signs = document.querySelectorAll('.hljs-button');
    for(var i=0;i<codes.length;i++){
        let sign = signs[i];
        csdn.modifyCopyPriviledge(codes[i], signs[i]);
        sign.addEventListener('click', function(){
            csdn.copy(this);
        },'true');
    };
})();