禁用CSDN动态背景

禁用CSDN动态背景,防止CPU占用过高

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         禁用CSDN动态背景
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  禁用CSDN动态背景,防止CPU占用过高
// @author       离尘zh、ChatGPT
// @match        https://blog.csdn.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    var hasAnimatedBackground = false;
    try {
        var element = document.querySelector('body');
        var styles = window.getComputedStyle(element);
        if (styles.backgroundImage.indexOf(".gif") !== -1) {
            hasAnimatedBackground = true;
        }
    } catch (e) {}

    if (hasAnimatedBackground) {
        var style = document.createElement('style');
        style.innerHTML = `
        * {
            animation: none !important;
            transition: none !important;
        }
        body {
            background-image: none !important;
        }
        `;
        document.head.appendChild(style);

        var observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                var addedNodes = mutation.addedNodes;
                addedNodes.forEach(function (node) {
                    if (node.nodeType !== Node.ELEMENT_NODE) {
                        return;
                    }

                    var nodeStyle = node.getAttribute("style");
                    if (nodeStyle != null) {
                        if (nodeStyle.includes("animation")) {
                            node.style.animation = "none";
                        }
                        if (nodeStyle.includes("transition")) {
                            node.style.transition = "none";
                        }
                    }

                    var bgStyle = node.style.backgroundImage;
                    if (bgStyle.indexOf(".gif") !== -1) {
                        node.style.backgroundImage = "none";
                    }
                });
            });
        });

        observer.observe(document.documentElement, {
            childList: true,
            subtree: true
        });
    }

})();