forked from TobiasMue91/tobiasmue91.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdark.js
More file actions
53 lines (43 loc) · 1.85 KB
/
Copy pathdark.js
File metadata and controls
53 lines (43 loc) · 1.85 KB
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
document.addEventListener('DOMContentLoaded', function() {
const DARK_MODE_CLASS = 'auto-dark-mode';
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
function applyDarkModeTransformations() {
const bodyChildren = document.body.children; // Get all children of the body
document.documentElement.classList.add(DARK_MODE_CLASS);
document.body.style.backgroundColor = '#121212';
// Apply the filter to each child of the body
Array.from(bodyChildren).forEach(child => {
child.style.filter = 'invert(0.85) hue-rotate(180deg)';
});
// Applying filter to media elements (img, video, iframe) to adjust for double inversion
const mediaElements = document.querySelectorAll('img, video, iframe');
mediaElements.forEach(el => {
el.style.filter = 'invert(0.85) hue-rotate(180deg)';
});
}
function removeDarkModeTransformations() {
const bodyChildren = document.body.children; // Get all children of the body
document.documentElement.classList.remove(DARK_MODE_CLASS);
document.body.style.backgroundColor = '';
// Remove the filter from each child of the body
Array.from(bodyChildren).forEach(child => {
child.style.filter = '';
});
// Remove filter from media elements
document.querySelectorAll('img, video, iframe').forEach(el => {
el.style.filter = '';
});
}
if (prefersDarkMode) {
applyDarkModeTransformations();
} else {
removeDarkModeTransformations();
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
if (event.matches) {
applyDarkModeTransformations();
} else {
removeDarkModeTransformations();
}
});
});