禁止网页更改标题栏颜色

强制PWA标题栏颜色,强制保持指定颜色

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 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         禁止网页更改标题栏颜色
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  强制PWA标题栏颜色,强制保持指定颜色
// @author       hiisme
// @match        https://www.icloud.com/photos/
// @match        https://www.xiaohongshu.com/*
// @match        https://chatglm.cn/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 配置不同网站的颜色
    const colorConfig = {
        'icloud.com': '#121212',
        'xiaohongshu.com': '#0A0A0A',
        'chatglm.cn': '#2B2E30',
    };

    // 获取当前网站的颜色配置
    function getColorForCurrentSite() {
        const hostname = window.location.hostname;
        for (const [site, color] of Object.entries(colorConfig)) {
            if (hostname.includes(site)) {
                return color;
            }
        }
        return '#121212'; // 默认颜色
    }

    // 设置或恢复标题栏颜色
    function enforceTitleBarColor() {
        let meta = document.querySelector('meta[name="theme-color"]');
        if (!meta) {
            meta = document.createElement('meta');
            meta.name = 'theme-color';
            document.head.appendChild(meta);
        }
        const color = getColorForCurrentSite();
        if (meta.content !== color) {
            meta.content = color;
        }
    }

    // 禁止对 theme-color 的不必要更改
    function blockThemeColorChanges() {
        const observer = new MutationObserver((mutations) => {
            mutations.forEach(mutation => {
                if (mutation.type === 'attributes' && mutation.attributeName === 'content') {
                    let target = mutation.target;
                    if (target.getAttribute('name') === 'theme-color') {
                        const color = getColorForCurrentSite();
                        if (target.content !== color) {
                            target.content = color; // 强制恢复颜色
                        }
                    }
                }
            });
        });

        const meta = document.querySelector('meta[name="theme-color"]');
        if (meta) {
            observer.observe(meta, { attributes: true });
        }
        enforceTitleBarColor(); // 初次设置颜色
    }

    // 防止网页动态创建或修改 theme-color 元素
    function interceptHeadModifications() {
        const originalAppendChild = HTMLElement.prototype.appendChild;
        HTMLElement.prototype.appendChild = function(element) {
            if (element.tagName === 'META' && element.name === 'theme-color') {
                element.content = getColorForCurrentSite(); // 根据网站设置颜色
            }
            return originalAppendChild.call(this, element);
        };
    }

    // 在页面加载前就开始拦截和观察
    interceptHeadModifications();
    document.addEventListener('DOMContentLoaded', blockThemeColorChanges);
})();