MCPcopy
hub / github.com/darkreader/darkreader / runDarkThemeDetector

Function runDarkThemeDetector

src/inject/detector.ts:149–188  ·  view source on GitHub ↗
(callback: (hasDarkTheme: boolean) => void, hints: DetectorHint[])

Source from the content-addressed store, hash-verified

147}
148
149export function runDarkThemeDetector(callback: (hasDarkTheme: boolean) => void, hints: DetectorHint[]): void {
150 stopDarkThemeDetector();
151 if (hints && hints.length > 0) {
152 const hint = hints[0];
153 if (hint.noDarkTheme) {
154 callback(false);
155 return;
156 }
157 if (hint.systemTheme && isSystemDarkModeEnabled()) {
158 callback(true);
159 return;
160 }
161 detectUsingHint(hint, () => callback(true));
162 return;
163 }
164
165 if (canCheckForStyle()) {
166 runCheck(callback);
167 return;
168 }
169
170 observer = new MutationObserver(() => {
171 if (canCheckForStyle()) {
172 stopDarkThemeDetector();
173 runCheck(callback);
174 }
175 });
176 observer.observe(document.documentElement, {childList: true});
177
178 if (document.readyState !== 'complete') {
179 readyStateListener = () => {
180 if (document.readyState === 'complete') {
181 stopDarkThemeDetector();
182 runCheck(callback);
183 }
184 };
185 // readystatechange event is not cancellable and does not bubble
186 document.addEventListener('readystatechange', readyStateListener);
187 }
188}
189
190export function stopDarkThemeDetector(): void {
191 if (observer) {

Callers 1

onMessageFunction · 0.90

Calls 5

isSystemDarkModeEnabledFunction · 0.90
stopDarkThemeDetectorFunction · 0.85
detectUsingHintFunction · 0.85
canCheckForStyleFunction · 0.85
runCheckFunction · 0.85

Tested by

no test coverage detected