|
1 | 1 | document.addEventListener('DOMContentLoaded', function() { |
2 | | - const body = document.querySelector('body'); |
| 2 | + const body = document.body; |
3 | 3 | const pictureEls = document.querySelectorAll('rht-picture'); |
4 | 4 |
|
| 5 | + let prefersDark = false; |
| 6 | + const colorScheme = localStorage.getItem('rhdsColorScheme'); |
| 7 | + |
| 8 | + if (!colorScheme || colorScheme === 'light dark') { |
| 9 | + if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { |
| 10 | + prefersDark = true; |
| 11 | + pictureEls.forEach((art) => { |
| 12 | + art.classList.add('dark'); |
| 13 | + }); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + if (colorScheme === 'dark') { |
| 18 | + pictureEls.forEach((art) => { |
| 19 | + art.classList.add('dark'); |
| 20 | + }); |
| 21 | + } else if (colorScheme === 'light') { |
| 22 | + pictureEls.forEach((art) => { |
| 23 | + art.classList.remove('dark'); |
| 24 | + }); |
| 25 | + } |
| 26 | + |
5 | 27 | var observer = new MutationObserver(function(mutations) { |
6 | 28 | mutations.forEach(function(mutation) { |
7 | | - if (mutation.type === "attributes") { |
8 | | - let styleAttr = mutation.target.getAttribute('style'); |
| 29 | + if (mutation.type === 'attributes' && mutation.attributeName === 'style') { |
| 30 | + const styleAttr = body.getAttribute('style'); |
9 | 31 |
|
10 | | - if (styleAttr.includes('color-scheme: dark')) { |
| 32 | + if (styleAttr === 'color-scheme: dark;') { |
11 | 33 | pictureEls.forEach((art) => { |
12 | 34 | art.classList.add('dark'); |
13 | 35 | }); |
14 | | - } else { |
| 36 | + } else if (styleAttr === 'color-scheme: light;') { |
15 | 37 | pictureEls.forEach((art) => { |
16 | 38 | art.classList.remove('dark'); |
17 | 39 | }); |
| 40 | + } else { |
| 41 | + if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { |
| 42 | + pictureEls.forEach((art) => { |
| 43 | + art.classList.add('dark'); |
| 44 | + }); |
| 45 | + } else { |
| 46 | + pictureEls.forEach((art) => { |
| 47 | + art.classList.remove('dark'); |
| 48 | + }); |
| 49 | + } |
18 | 50 | } |
19 | 51 | } |
20 | 52 | }); |
|
0 commit comments