MCPcopy Index your code
hub / github.com/darkreader/darkreader / updateAutoState

Method updateAutoState

src/background/extension.ts:153–210  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

151 }
152
153 private static updateAutoState() {
154 const {mode, behavior, enabled} = UserStorage.settings.automation;
155
156 let isAutoDark: boolean | null | undefined;
157 let nextCheck: number | null | undefined;
158 switch (mode) {
159 case AutomationMode.TIME: {
160 const {time} = UserStorage.settings;
161 isAutoDark = isInTimeIntervalLocal(time.activation, time.deactivation);
162 nextCheck = nextTimeInterval(time.activation, time.deactivation);
163 break;
164 }
165 case AutomationMode.SYSTEM:
166 if (__CHROMIUM_MV3__) {
167 isAutoDark = Extension.wasLastColorSchemeDark;
168 if (Extension.wasLastColorSchemeDark === null) {
169 logWarn('System color scheme is unknown. Defaulting to Dark.');
170 isAutoDark = true;
171 }
172 break;
173 }
174 isAutoDark = Extension.wasLastColorSchemeDark === null
175 ? isSystemDarkModeEnabled()
176 : Extension.wasLastColorSchemeDark;
177 if (isFirefox) {
178 runColorSchemeChangeDetector(Extension.onColorSchemeChange);
179 }
180 break;
181 case AutomationMode.LOCATION: {
182 const {latitude, longitude} = UserStorage.settings.location;
183 if (latitude != null && longitude != null) {
184 isAutoDark = isNightAtLocation(latitude, longitude);
185 nextCheck = nextTimeChangeAtLocation(latitude, longitude);
186 }
187 break;
188 }
189 case AutomationMode.NONE:
190 break;
191 }
192
193 let state: AutomationState = '';
194 if (enabled) {
195 if (behavior === 'OnOff') {
196 state = isAutoDark ? 'turn-on' : 'turn-off';
197 } else if (behavior === 'Scheme') {
198 state = isAutoDark ? 'scheme-dark' : 'scheme-light';
199 }
200 }
201 Extension.autoState = state;
202
203 if (nextCheck) {
204 if (nextCheck < Date.now()) {
205 logWarn(`Alarm is set in the past: ${nextCheck}. The time is: ${new Date()}. ISO: ${(new Date()).toISOString()}`);
206 } else {
207 chrome.alarms.create(Extension.ALARM_NAME, {when: nextCheck});
208 }
209 }
210 }

Callers 3

startMethod · 0.80
ExtensionClass · 0.80
changeSettingsMethod · 0.80

Calls 7

isInTimeIntervalLocalFunction · 0.90
nextTimeIntervalFunction · 0.90
logWarnFunction · 0.90
isSystemDarkModeEnabledFunction · 0.90
isNightAtLocationFunction · 0.90
nextTimeChangeAtLocationFunction · 0.90

Tested by

no test coverage detected