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 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();