油猴CSDN凈化腳本


CSDN版面越來越亂,最近還總是彈出紅包雨和頂部巨大橫幅,左側也會隨機出現學生認證彈窗。而且版面混亂難看,看起來非常費勁。
另外底下的推薦列表經常夾雜着CSDN文件下載的鏈接,下載文件又要付費,從來不會用CSDN的下載,只希望能在推薦列表里看看博客,所以順便把底下推薦列表中的下載推薦鏈接也去掉了,只剩下博客的鏈接。
所以寫了一個凈化CSDN的油猴腳本,效果還可以。

CSDN博客默認打開效果:

使用腳本凈化后的效果:

0.6.3 加入目錄后的效果:

*0.5.1更新(2021-11-02):右側邊欄去除

*0.5.2更新(2022-07-03):頂欄左側按鈕錯位修復

*0.6.4更新(2023-02-09):左側保留CSDN自帶目錄,閱讀長文更方便

*0.7.0更新(2023-04-16):1.刪除右上角創作按鈕的動畫廣告 2.代碼免登錄復制

油猴(TamperMonkey)里創建一個新腳本,直接復制進去就能用。

// ==UserScript==
// @name         CSDN凈化
// @namespace    http://tampermonkey.net/
// @version      0.7.0
// @description  fuck you csdn!
// @author       Sanders
// @match        *://blog.csdn.net/*
// @match        *://bbs.csdn.net/*
// @icon         https://blog.csdn.net/favicon.ico
// @grant        none
// @run-at       document-end
// ==/UserScript==


(function() {
    'use strict';

    // 背景調成深色,看起來更舒適
    $("body").css("background-color", "dimgray");


    // 刪除側邊欄熱門文章
    var hotArticle = document.getElementById("asideHotArticle");
    if (hotArticle != null) {
        hotArticle.remove();
    }

    // 右側的目錄
    const content = document.getElementById("groupfile");


    //document.getElementById("asidedirectory"); // 左側目錄,不穩定,窄頁面會顯示,頁面變寬就不顯示了
    //document.getElementById("groupfile");      // 右側目錄,穩定,始終會顯示


    // 直接刪除左側邊欄
    const sideBar = document.getElementsByClassName("blog_container_aside")[0]
    if (sideBar != null) {
        sideBar.remove();
    }

    // 刪除右側邊欄
    const rightSideBar = document.getElementById("rightAside");
    if (rightSideBar != null) {
        rightSideBar.remove();
    }

    if (content != null) {
        // 不設置這個目錄不能滾動
        content.setAttribute("style", "overflow: scroll;");
        // 設置高度
        //content.setAttribute("style", "height: fit-content;");

        // 創建一個新的左邊欄
        var blogContainerAside = document.createElement("aside");
        // 設置class為左邊欄的
        blogContainerAside.setAttribute("class", "blog_container_aside");
        // 位置固定在頂部
        blogContainerAside.setAttribute("style", "position: fixed; top: 56px; left: 5%");
        // 把之前保存的目錄塞進新左邊欄
        blogContainerAside.appendChild(content);


        // 添加左邊欄到mainBox
        document.getElementById("mainBox").appendChild(blogContainerAside);
        // 設置左側間距, 9%左右剛好能讓正文居中,讓視覺中心看在正文上,平衡不受左側目錄影響
        document.getElementById("mainBox").setAttribute("style", "margin-left: 8.5%");
        // 取消這個father的居中
        document.getElementsByClassName("main_father")[0].setAttribute("style", "justify-content: start !important");

    }



    // 讓文章居中全寬(這個其實不是mainBox, 是mainBox下面的<main>標簽)
    var mainBox = document.querySelector('#mainBox>main');
    if (mainBox != null) {

        if (content == null) {
            mainBox.style.float = "none";
            mainBox.style.marginLeft = "12.5%";
        }
        mainBox.style.width = '75%';
        // 浮動在最前,要能壓住左側的目錄邊欄
        mainBox.style.zIndex = '1';
        mainBox.style.position = 'relative';


        // 調整圖片比例
        $("#content_views img").css("max-width", "65%");
        $("#content_views img").css("margin-left", "22.5%");
    }


    // 將底部的作者欄調小
    var bottomBar = document.getElementsByClassName("left-toolbox")[0]
    if (bottomBar != null) {
        bottomBar.style.height = "10px";
    }

    // 邊欄移到底部去
    // document.getElementsByClassName("blog_container_aside")[0].style.display = "contents"

    // 刪除所有download的鏈接(僅適用於blog.csdn.net)
    var downloads = document.getElementsByClassName("recommend-item-box type_download");
    // 反着刪才管用
    for(var i=downloads.length - 1; i >= 0; i--){
        if (downloads[i] != null) {
            downloads[i].remove();
        }
    }


    // 刪除所有download.csdn.net的鏈接
    setTimeout(function() {

        var downloads2 = document.querySelectorAll("div[data-type=download]");
        for(i=downloads2.length - 1; i >= 0; i--){
            if (downloads2[i] != null) {
                downloads2[i].remove();
            }
        }


        var allLinks = document.getElementsByTagName("a");
        var downloadReg = RegExp(/download.csdn.net/);
        for (i = allLinks.length - 1; i >= 0; i--) {
            const link = allLinks[i].href;
            if (link.match(downloadReg)) {
                // 為了判斷是不是導航欄的下載按鈕,如果刪了會導致導航欄錯位,很蠢
                if (allLinks[i].parentElement.title != "獲取源碼、文檔、學習資源") {
                    allLinks[i].remove();
                }
                //allLinks[i].remove();
            }
        }
    }, 1000);


    // 刪除學生認證
    // 每1秒檢測一次,持續檢測10次,有時候網速問題加載會延時
    var studentTime = 0;
    var student = setInterval(deleteStudent, 1000);

    function deleteStudent() {
        var highschool = document.getElementById("csdn-highschool-window");
        if (highschool != null) {
            highschool.remove();
            clearInterval(student);
        }

        if (studentTime == 10) {
            clearInterval(student);
        }
        studentTime++;
    }


    // 針對blink.csdn.net頁面調整
    // 調整版面
    $(".blink-main-l").css("width", "70%");
    $(".blink-main-l").css("margin-left", "15%");
    $(".blink-main-l").css("margin-right", "0");
    // 移除右邊的個人欄
    $(".blink-main-r").remove();


    // 針對bbs.csdn.net頁面調整
    $(".user-right-floor").remove();
    $(".detail-container").css("margin-left", "15%");



    window.onload = function () {

        // 目錄高度限制放大一點
        document.getElementsByClassName("groupfile-div")[0].setAttribute("style", "max-height: 500px");

        // 刪除頂欄廣告
        var adTime = 10;
        var adBar = setInterval(removeAdBar, 1000);
        function removeAdBar() {

            var adBar = document.getElementsByClassName("toolbar-advert")[0];
            if (adBar != null) {
                adBar.remove();
                clearInterval(adBar);
            }
            if (time == 10) {
                clearInterval(adBar);
            }
            time++;

        }


        // 刪除其他廣告(針對CSDN主頁)
        $("[id^=kp_box]").remove();
        $("[class*=advert-box]").remove(); // 會導致頂欄錯位

        // 刪除學生認證
        var highschool = document.getElementById("csdn-highschool-window");
        if (highschool != null) {
            highschool.remove();
        }

        // 刪除右下角的圓形廣告
        var toolbar = document.getElementsByClassName("csdn-side-toolbar")[0];
        if (toolbar != null) {
            toolbar.remove();
        }
        var logo_ad = document.getElementsByClassName("csdn-common-logo-advert")[0];
        if (logo_ad != null) {
            logo_ad.remove();
        }

        // 刪除Logo
        var logo = document.getElementsByClassName("toolbar-logo")[0];
        if (logo != null) {
            logo.remove();
        }

        // 刪除創作按鈕的廣告
        // 選中按鈕下面的a標簽
        var writeButtonImage = document.querySelector('.toolbar-btn-write a');
        if (writeButtonImage != null) {
            // 刪掉“has-image”的class
            writeButtonImage.className = "";
            // 添加文字
            writeButtonImage.innerText = "創作"
        }


        // 頂部左側按鈕的 height=100% 會錯位,移除這個屬性就好了
        var tool_bar = document.getElementsByClassName("toolbar-menus")[0];
        if (tool_bar != null) {
            tool_bar.style.height = "auto";
        }


        // 刪除vip彈窗廣告
        var vip = $(".mask")[0]
        if (vip != null) {
            vip.remove();
        }

        // 刪除紅包雨
        // 每0.5秒檢測一次,持續檢測4次,有時候網速問題加載會延時
        var redTime = 0;
        var redPocket = setInterval(deleteRedPocket, 500);
        function deleteRedPocket() {
            var redPocketLayer = document.getElementById("csdn-redpack");
            if (redPocketLayer != null) {
                redPocketLayer.remove();
                clearInterval(redPocket);
            }
            if (redTime == 4) {
                clearInterval(redPocket);
            }
            redTime++;
        }

        // 刪除會員組合券廣告彈窗
        var buysideTime = 0;
        var buyside = setInterval(deleteBuyside, 500);
        function deleteBuyside() {
            var buysideLayer = document.getElementByClassName("csdn-buyside-entry-box")[0];
            if (buysideLayer != null) {
                buysideLayer.remove();
                clearInterval(buyside);
            }
            if (redTime == 4) {
                clearInterval(buyside);
            }
            buysideTime++;
        }



        // 刪除登錄彈窗
        // 每1秒檢測一次,持續檢測10次,有時候網速問題加載會延時
        var time = 0;
        var login = setInterval(deleteLogin, 1000);

        function deleteLogin() {
            var loginWindow = document.getElementsByClassName('passport-login-container')[0];
            if (loginWindow != null) {
                loginWindow.remove();
                clearInterval(login);
            }
            if (time == 10) {
                clearInterval(login);
            }
            time++;
        }


        // 免登錄復制
        $(".hljs-button").removeClass("signin");
        $(".hljs-button").attr("data-title", "免登錄復制");
        $(".hljs-button").attr(
            "onclick",
            "hljs.copyCode(event);setTimeout(function(){$('.hljs-button').attr('data-title', '免登錄復制');},3500);"
        );
        // 去除剪貼板劫持
        $("code").attr("onclick", "mdcp.copyCode(event)");
        try {
            Object.defineProperty(window, "articleType", {
                value: 0,
                writable: false,
                configurable: false,
            });

            csdn.copyright.init("", "", "");
        } catch (err) {}

    };
})();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM