Modify the `enwikimwclientpreferences` cookie once per session, to instantly set preferences, and change classes, so no reload is required.
目前為
// ==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();
})();