()
| 488 | } |
| 489 | |
| 490 | function 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 | |
| 539 | function stopWatchingForUpdates() { |
| 540 | styleManagers.forEach((manager) => manager.pause()); |
no test coverage detected