屏蔽博客园和CSDN动态背景

移除博客园和CSDN页面上的动态背景和特效

目前为 2024-06-06 提交的版本。查看 最新版本

// ==UserScript==
// @name         屏蔽博客园和CSDN动态背景
// @namespace    https://www.cnblogs.com/
// @version      0.2
// @description  移除博客园和CSDN页面上的动态背景和特效
// @author       yepengyu
// @match        *://*.cnblogs.com/*
// @match        https://blog.csdn.net/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    console.log("Tampermonkey script loaded");

    // Function to remove the particle effect scripts and canvases
    function removeParticleEffects() {
        // Get all script elements
        var scripts = document.getElementsByTagName('script');

        // Iterate over the script elements
        for (var i = 0; i < scripts.length; i++) {
            var script = scripts[i];

            // Check if the script contains particle effect initialization
            if (script.src.includes("particles.min.js") || script.innerHTML.includes("particlesJS")) {
                script.remove();
                console.log("Removed particle effect script:", script);
            }
        }

        // Remove canvas elements related to particle effects
        var canvases = document.getElementsByTagName('canvas');
        for (var j = 0; j < canvases.length; j++) {
            var canvas = canvases[j];
            canvas.remove();
            console.log("Removed canvas element:", canvas);
        }
    }

    // Function to remove the dynamic background images on CSDN
    function removeCSDNBackground() {
        // 图片链接
        var blockedImageUrl = 'https://csdnimg.cn/release/blogv2/dist/pc/themesSkin/skin-whitemove/images/bg.gif';

        // 获取所有图片元素
        var images = document.querySelectorAll('img');

        // 遍历图片元素
        for (var i = 0; i < images.length; i++) {
            var image = images[i];

            // 检查图片链接是否与被屏蔽的链接相同
            if (image.src === blockedImageUrl) {
                // 隐藏图片
                image.style.display = 'none';
                console.log("Blocked an image with src: " + blockedImageUrl);
            }
        }
    }

    // Detect if the site is CSDN or cnblogs and run the respective function
    if (window.location.host.indexOf('cnblogs.com') > -1) {
        console.log("Running script for cnblogs");
        // Run the function to remove particle effects
        removeParticleEffects();

        // Observe for added scripts dynamically
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (mutation.addedNodes.length) {
                    removeParticleEffects();
                }
            });
        });

        // Observe changes in the document
        observer.observe(document.documentElement, { childList: true, subtree: true });

    } else if (window.location.host.indexOf('csdn.net') > -1) {
        console.log("Running script for csdn");
        // Run the function to remove CSDN background
        removeCSDNBackground();
    }

})();