|
10 | 10 | ------------------------*/ |
11 | 11 | var ncss, dcss = document.getElementsByTagName("link")[25]; |
12 | 12 | var nightTheme = '#111', dayTheme = '#fff'; |
13 | | -var nightChangeEvent = new CustomEvent('night.changed'); |
| 13 | +var nightChangeEvent = new CustomEvent('night.changed'), nightListener; |
14 | 14 |
|
15 | 15 | function night() { |
16 | | - if (firetext.settings.get('nightmode') == 'true') { |
17 | | - startNight(true); |
18 | | - } else if (firetext.settings.get('nightmode') == 'false') { |
19 | | - startNight(false); |
20 | | - } else { |
21 | | - startNight(false); |
22 | | - |
23 | | - window.addEventListener('devicelight', function(event) { |
24 | | - if (firetext.settings.get('nightmode') == 'auto') { |
25 | | - if (event.value < 3.4) { |
26 | | - if (html.classList.contains('night') != true) { |
27 | | - startNight(true); |
28 | | - } |
29 | | - } else if (event.value > 5) { |
30 | | - if (html.classList.contains('night')) { |
31 | | - startNight(false); |
32 | | - } |
33 | | - } |
| 16 | + switch (firetext.settings.get('nightmode')) { |
| 17 | + case "true": |
| 18 | + startNight(true); |
| 19 | + window.removeEventListener('devicelight', processLight); |
| 20 | + nightListener = false; |
| 21 | + break; |
| 22 | + case "false": |
| 23 | + startNight(false); |
| 24 | + window.removeEventListener('devicelight', processLight); |
| 25 | + nightListener = false; |
| 26 | + break; |
| 27 | + case "auto": |
| 28 | + startNight(false); |
| 29 | + if (!nightListener) window.addEventListener('devicelight', processLight); |
| 30 | + nightListener = true; |
| 31 | + break; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function processLight(event) { |
| 36 | + if (firetext.settings.get('nightmode') == 'auto') { |
| 37 | + if (event.value < 3.4) { |
| 38 | + if (html.classList.contains('night') != true) { |
| 39 | + startNight(true); |
| 40 | + } |
| 41 | + } else if (event.value > 5) { |
| 42 | + if (html.classList.contains('night')) { |
| 43 | + startNight(false); |
34 | 44 | } |
35 | | - }); |
| 45 | + } |
36 | 46 | } |
37 | 47 | } |
38 | 48 |
|
|
0 commit comments