MCPcopy
hub / github.com/darkreader/darkreader / watchForUpdates

Function watchForUpdates

src/inject/dynamic-theme/index.ts:490–537  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

488}
489
490function watchForUpdates() {
491 const managedStyles = Array.from(styleManagers.keys());
492 watchForStyleChanges(managedStyles, ({created, updated, removed, moved}) => {
493 const stylesToRemove = removed;
494 const stylesToManage = created.concat(updated).concat(moved)
495 .filter((style) => !styleManagers.has(style));
496 const stylesToRestore = moved
497 .filter((style) => styleManagers.has(style));
498 logInfo(`Styles to be removed:`, stylesToRemove);
499 stylesToRemove.forEach((style) => removeManager(style));
500 const newManagers = stylesToManage
501 .map((style) => createManager(style));
502 newManagers
503 .map((manager) => manager.details({secondRound: false}))
504 .filter((detail) => detail && detail.rules.length > 0)
505 .forEach((detail) => {
506 variablesStore.addRulesForMatching(detail!.rules);
507 });
508 variablesStore.matchVariablesAndDependents();
509 newManagers.forEach((manager) => manager.render(theme!, ignoredImageAnalysisSelectors));
510 newManagers.forEach((manager) => manager.watch());
511 stylesToRestore.forEach((style) => styleManagers.get(style)!.restore());
512 }, (shadowRoot) => {
513 createShadowStaticStyleOverrides(shadowRoot);
514 handleAdoptedStyleSheets(shadowRoot);
515 });
516
517 watchForInlineStyles((element) => {
518 overrideInlineStyle(element, theme!, ignoredInlineSelectors, ignoredImageAnalysisSelectors);
519 if (element === document.documentElement) {
520 const styleAttr = element.getAttribute('style') || '';
521 if (styleAttr.includes('--')) {
522 variablesStore.matchVariablesAndDependents();
523 const rootVarsStyle = createOrUpdateStyle('darkreader--root-vars');
524 variablesStore.putRootVars(rootVarsStyle, theme!);
525 }
526 }
527 }, (root) => {
528 createShadowStaticStyleOverrides(root);
529 const inlineStyleElements = root.querySelectorAll(INLINE_STYLE_SELECTOR) as NodeListOf<HTMLElement>;
530 if (inlineStyleElements.length > 0) {
531 forEach(inlineStyleElements, (el: HTMLElement) => overrideInlineStyle(el, theme!, ignoredInlineSelectors, ignoredImageAnalysisSelectors));
532 }
533 });
534
535 addDOMReadyListener(onDOMReady);
536 setupDocumentPiPFontFix();
537}
538
539function stopWatchingForUpdates() {
540 styleManagers.forEach((manager) => manager.pause());

Callers 1

runDynamicStyleFunction · 0.85

Calls 15

watchForStyleChangesFunction · 0.90
logInfoFunction · 0.90
watchForInlineStylesFunction · 0.90
overrideInlineStyleFunction · 0.90
forEachFunction · 0.90
addDOMReadyListenerFunction · 0.90
removeManagerFunction · 0.85
createManagerFunction · 0.85
handleAdoptedStyleSheetsFunction · 0.85
setupDocumentPiPFontFixFunction · 0.85
detailsMethod · 0.80

Tested by

no test coverage detected