-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniversal-DarkMode2.user.js
74 lines (61 loc) · 2.57 KB
/
Universal-DarkMode2.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ==UserScript==
// @name Universal Dark Mode 2 (Chrome-like)
// @match *://*/*
// @grant none
// @version 1.5
// @author almahmud & gpt
// @homepageURL https://github.com/almahmudbd/universal-dark-mode
// @description Chrome-like dark mode with adjusted colors for better readability.
// @license GPL-2.0
// @run-at document-start
// @namespace https://greasyfork.org/users/1238578
// @downloadURL https://update.greasyfork.org/scripts/512895/Universal%20Dark%20Mode%202%20%28Chrome-like%29.user.js
// @updateURL https://update.greasyfork.org/scripts/512895/Universal%20Dark%20Mode%202%20%28Chrome-like%29.user.js
// ==/UserScript==
const setDarkModeCookie = (value) => {
document.cookie = `darkMode=${value}; path=/; max-age=2592000`; // 1 month expiration
};
const getDarkModeCookie = () => {
const match = document.cookie.match(/darkMode=([^;]+)/);
return match ? match[1] === 'true' : false; // Default to false
};
const isDarkMode = getDarkModeCookie();
// Set a base background color to prevent flash
document.documentElement.style.backgroundColor = "#263238"; // Dark grey-blue color for background
const applyDarkModeStyles = () => {
const elements = document.querySelectorAll("*");
elements.forEach((element) => {
const computedStyle = window.getComputedStyle(element);
const backgroundColor = computedStyle.backgroundColor;
// Apply dark background to all elements except images and videos
if (!backgroundColor.includes("rgba") && !backgroundColor.includes("transparent")) {
element.style.backgroundColor = "#263238";
}
// Change text color to white
element.style.color = "#ffffff";
});
};
// Apply dark mode immediately if cookie is set
if (isDarkMode) {
applyDarkModeStyles();
}
// Adjust colors function
const adjustColors = () => {
const elements = document.querySelectorAll("*");
elements.forEach((element) => {
const computedStyle = window.getComputedStyle(element);
if (computedStyle.backgroundColor !== "rgba(0, 0, 0, 0)" &&
computedStyle.backgroundColor !== "transparent") {
element.style.backgroundColor = "#263238"; // Dark grey-blue for background
}
element.style.color = "#ffffff"; // White text color
});
};
// Adjust colors when the document is ready
document.onreadystatechange = () => {
if (isDarkMode) {
adjustColors();
}
};
// Observe changes in the DOM
new MutationObserver(adjustColors).observe(document.body, { subtree: true, childList: true });