禁用CSDN动态背景

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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
        });
    }

})();