(message: MessageCStoBG | MessageCStoUI)
| 44 | } |
| 45 | |
| 46 | function sendMessage(message: MessageCStoBG | MessageCStoUI): true | undefined { |
| 47 | if (unloaded) { |
| 48 | return; |
| 49 | } |
| 50 | const responseHandler = (response: MessageBGtoCS | 'unsupportedSender' | undefined) => { |
| 51 | // Vivaldi bug workaround. See TabManager for details. |
| 52 | if (response === 'unsupportedSender' || response?.type === MessageTypeBGtoCS.UNSUPPORTED_SENDER) { |
| 53 | removeStyle(); |
| 54 | removeSVGFilter(); |
| 55 | removeDynamicTheme(); |
| 56 | cleanup(); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | try { |
| 61 | if (__CHROMIUM_MV3__) { |
| 62 | const promise = chrome.runtime.sendMessage<MessageCStoBG | MessageCStoUI, MessageBGtoCS | 'unsupportedSender'>(message); |
| 63 | promise.then(responseHandler).catch(cleanup); |
| 64 | } else { |
| 65 | chrome.runtime.sendMessage<MessageCStoBG | MessageCStoUI, 'unsupportedSender' | undefined>(message, responseHandler); |
| 66 | } |
| 67 | } catch (error: any) { |
| 68 | /* |
| 69 | * We get here if Background context is unreachable which occurs when: |
| 70 | * - extension was disabled |
| 71 | * - extension was uninstalled |
| 72 | * - extension was updated and this is the old instance of content script |
| 73 | * |
| 74 | * Any async operations can be ignored here, but sync ones should run to completion. |
| 75 | * |
| 76 | * Regular message passing errors are returned via rejected promise or runtime.lastError. |
| 77 | */ |
| 78 | if (error.message === 'Extension context invalidated.') { |
| 79 | console.log('Dark Reader: instance of old CS detected, cleaning up.'); |
| 80 | cleanup(); |
| 81 | } else { |
| 82 | console.log('Dark Reader: unexpected error during message passing.'); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function onMessage(message: MessageBGtoCS | MessageUItoCS | DebugMessageBGtoCS) { |
| 88 | if (__DEBUG__ && message.type === DebugMessageTypeBGtoCS.RELOAD) { |
no test coverage detected