Greasy Fork

Greasy Fork is available in English.

禁止网页更改标题栏颜色

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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