Wikipedia - Keeps style preferences in incognito

Modify the `enwikimwclientpreferences` cookie once per session, to instantly set preferences, and change classes, so no reload is required.

当前为 2025-02-04 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Wikipedia - Keeps style preferences in incognito
// @namespace    http://tampermonkey.net/
// @version      2025-02-04
// @description  Modify the `enwikimwclientpreferences` cookie once per session, to instantly set preferences, and change classes, so no reload is required.
// @author       jackiechan285
// @match        https://en.wikipedia.org/*
// @match        https://*.wikipedia.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wikipedia.org
// @grant        GM_cookie
// ==/UserScript==

(function() {
    'use strict';

    // Check if the script has already run in this session
    if (localStorage.getItem('cookieModified') === 'true') {
        return; // Exit if already modified
    }

    // Check if the cookie exists
    GM_cookie.list({ name: 'enwikimwclientpreferences' }, function(cookies, error) {
        if (error) {
            console.error('Error retrieving cookies:', error);
            return;
        }

        if (cookies && cookies.length > 0) {
            // Cookie exists, modify it
            const newCookieValue = "skin-theme-clientpref-night%2Cvector-feature-limited-width-clientpref-0%2Cvector-feature-custom-font-size-clientpref-0%2Cvector-feature-appearance-pinned-clientpref-0";

            GM_cookie.set({
                name: 'enwikimwclientpreferences',
                value: newCookieValue,
                path: '/',
                secure: true,
                expirationDate: Math.floor(Date.now() / 1000) + (60 * 60 * 24) // Expires in 1 day
            }, function(error) {
                if (error) {
                    console.error('Error setting cookie:', error);
                } else {
                    console.log('Cookie updated successfully.');
                }
            });

            // Set a flag in localStorage to indicate the script has run
            localStorage.setItem('cookieModified', 'true');
        } else {
            console.log('Cookie enwikimwclientpreferences does not exist.');
        }
    });

    // Replace specific classes in HTML elements
    const replaceClasses = () => {
        // Get all elements in the document
        const elements = document.querySelectorAll('*');

        // Loop through each element
        elements.forEach((element) => {
            // Replace classes that match the patterns
            element.classList.forEach((className) => {
                if (className.includes('vector-feature-limited-width-clientpref-')) {
                    element.classList.replace(className, 'vector-feature-limited-width-clientpref-0');
                }
                if (className.includes('vector-feature-custom-font-size-clientpref-')) {
                    element.classList.replace(className, 'vector-feature-custom-font-size-clientpref-0');
                }
                if (className.includes('skin-theme-clientpref-')) {
                    element.classList.replace(className, 'skin-theme-clientpref-night');
                }
                if (className.includes('vector-feature-appearance-pinned-clientpref-')) {
                    element.classList.replace(className, 'vector-feature-appearance-pinned-clientpref-1');
                }
            });
        });
    };

    // Run the class replacement
    replaceClasses();
})();