MCPcopy
hub / github.com/darkreader/darkreader / onMessage

Function onMessage

src/inject/index.ts:87–167  ·  view source on GitHub ↗
(message: MessageBGtoCS | MessageUItoCS | DebugMessageBGtoCS)

Source from the content-addressed store, hash-verified

85}
86
87function onMessage(message: MessageBGtoCS | MessageUItoCS | DebugMessageBGtoCS) {
88 if (__DEBUG__ && message.type === DebugMessageTypeBGtoCS.RELOAD) {
89 logWarn('Cleaning up before update');
90 cleanup();
91 return;
92 }
93
94 if ((message as MessageBGtoCS).scriptId !== scriptId && message.type !== MessageTypeUItoCS.EXPORT_CSS) {
95 return;
96 }
97
98 logInfoCollapsed(`onMessage[${message.type}]`, message);
99 switch (message.type) {
100 case MessageTypeBGtoCS.ADD_CSS_FILTER:
101 case MessageTypeBGtoCS.ADD_STATIC_THEME: {
102 const {css, detectDarkTheme, detectorHints} = message.data;
103 removeDynamicTheme();
104 createOrUpdateStyle(css, message.type === MessageTypeBGtoCS.ADD_STATIC_THEME ? 'static' : 'filter');
105 writeEnabledForHost(true);
106 if (detectDarkTheme) {
107 runDarkThemeDetector((hasDarkTheme) => {
108 if (hasDarkTheme) {
109 removeStyle();
110 onDarkThemeDetected();
111 }
112 }, detectorHints);
113 }
114 break;
115 }
116 case MessageTypeBGtoCS.ADD_SVG_FILTER: {
117 const {css, svgMatrix, svgReverseMatrix, detectDarkTheme, detectorHints} = message.data;
118 removeDynamicTheme();
119 createOrUpdateSVGFilter(svgMatrix, svgReverseMatrix);
120 createOrUpdateStyle(css, 'filter');
121 writeEnabledForHost(true);
122 if (detectDarkTheme) {
123 runDarkThemeDetector((hasDarkTheme) => {
124 if (hasDarkTheme) {
125 removeStyle();
126 removeSVGFilter();
127 onDarkThemeDetected();
128 }
129 }, detectorHints);
130 }
131 break;
132 }
133 case MessageTypeBGtoCS.ADD_DYNAMIC_THEME: {
134 const {theme, fixes, isIFrame, detectDarkTheme, detectorHints} = message.data;
135 removeStyle();
136 createOrUpdateDynamicTheme(theme, fixes, isIFrame);
137 writeEnabledForHost(true);
138 if (detectDarkTheme) {
139 runDarkThemeDetector((hasDarkTheme) => {
140 if (hasDarkTheme) {
141 removeDynamicTheme();
142 onDarkThemeDetected();
143 }
144 }, detectorHints);

Callers

nothing calls this directly

Calls 15

logWarnFunction · 0.90
logInfoCollapsedFunction · 0.90
removeDynamicThemeFunction · 0.90
createOrUpdateStyleFunction · 0.90
writeEnabledForHostFunction · 0.90
runDarkThemeDetectorFunction · 0.90
removeStyleFunction · 0.90
createOrUpdateSVGFilterFunction · 0.90
removeSVGFilterFunction · 0.90
collectCSSFunction · 0.90
stopDarkThemeDetectorFunction · 0.90

Tested by

no test coverage detected