MCPcopy
hub / github.com/darkreader/darkreader / sendMessage

Function sendMessage

src/inject/color-scheme-watcher.ts:11–40  ·  view source on GitHub ↗
(message: MessageCStoBG)

Source from the content-addressed store, hash-verified

9}
10
11function sendMessage(message: MessageCStoBG): void {
12 const responseHandler = (response: MessageBGtoCS | 'unsupportedSender' | undefined) => {
13 // Vivaldi bug workaround. See TabManager for details.
14 if (response === 'unsupportedSender') {
15 cleanup();
16 }
17 };
18
19 try {
20 const promise = chrome.runtime.sendMessage<MessageCStoBG, MessageBGtoCS | 'unsupportedSender'>(message);
21 promise.then(responseHandler).catch(cleanup);
22 } catch (error) {
23 /*
24 * We get here if Background context is unreachable which occurs when:
25 * - extension was disabled
26 * - extension was uninstalled
27 * - extension was updated and this is the old instance of content script
28 *
29 * Any async operations can be ignored here, but sync ones should run to completion.
30 *
31 * Regular message passing errors are returned via rejected promise or runtime.lastError.
32 */
33 if ((error as Error).message === 'Extension context invalidated.') {
34 console.log('Dark Reader: instance of old CS detected, cleaning up.');
35 cleanup();
36 } else {
37 console.log('Dark Reader: unexpected error during message passing.');
38 }
39 }
40}
41
42function notifyOfColorScheme(isDark: boolean): void {
43 sendMessage({type: MessageTypeCStoBG.COLOR_SCHEME_CHANGE, data: {isDark}});

Callers 1

notifyOfColorSchemeFunction · 0.70

Calls 2

sendMessageMethod · 0.80
cleanupFunction · 0.70

Tested by

no test coverage detected