(tabId: number, documentId: string, message: MessageBGtoCS, frameId: number)
| 256 | } |
| 257 | |
| 258 | private static sendDocumentMessage(tabId: number, documentId: string, message: MessageBGtoCS, frameId: number) { |
| 259 | if (frameId === 0) { |
| 260 | const themeMessageTypes: MessageTypeBGtoCS[] = [ |
| 261 | MessageTypeBGtoCS.ADD_CSS_FILTER, |
| 262 | MessageTypeBGtoCS.ADD_DYNAMIC_THEME, |
| 263 | MessageTypeBGtoCS.ADD_STATIC_THEME, |
| 264 | MessageTypeBGtoCS.ADD_SVG_FILTER, |
| 265 | ]; |
| 266 | if (themeMessageTypes.includes(message.type)) { |
| 267 | IconManager.setIcon({tabId, isActive: true, colorScheme: message.data?.theme?.mode ? 'dark' : 'light'}); |
| 268 | } else if (message.type === MessageTypeBGtoCS.CLEAN_UP) { |
| 269 | const isActive = TabManager.tabs[tabId]?.[0]?.url?.startsWith('https://darkreader.org/'); |
| 270 | IconManager.setIcon({tabId, isActive}); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (__CHROMIUM_MV3__) { |
| 275 | // On MV3, Chromium has a bug which prevents sending messages to pre-rendered frames without specifying frameId |
| 276 | // Furthermore, if we send a message addressed to a temporary frameId after the document exits prerender state, |
| 277 | // the message will also fail to be delivered. |
| 278 | // |
| 279 | // To work around this: |
| 280 | // 1. Attempt to send the message by documentId. If this fails, this means the document is in prerender state. |
| 281 | // 2. Attempt to send the message by documentId and temporary frameId. If this fails, this means the document |
| 282 | // either already exited pre-rendered state or was discarded. |
| 283 | // 3. Attempt to send the message by documentId (omitting the permanent frameId which is 0).If this fails, this |
| 284 | // means the document was already discarded. |
| 285 | // |
| 286 | // More info: https://crbug.com/1455817 |
| 287 | |
| 288 | chrome.tabs.sendMessage<MessageBGtoCS>(tabId, message, {documentId}).catch(() => |
| 289 | chrome.tabs.sendMessage<MessageBGtoCS>(tabId, message, {frameId, documentId}).catch(() => |
| 290 | chrome.tabs.sendMessage<MessageBGtoCS>(tabId, message, {documentId}).catch(() => { /* noop */ }) |
| 291 | ) |
| 292 | ); |
| 293 | return; |
| 294 | } |
| 295 | if (__CHROMIUM_MV2__) { |
| 296 | chrome.tabs.sendMessage<MessageBGtoCS>(tabId, message, documentId ? {documentId} : {frameId}); |
| 297 | return; |
| 298 | } |
| 299 | chrome.tabs.sendMessage<MessageBGtoCS>(tabId, message, {frameId}); |
| 300 | } |
| 301 | |
| 302 | private static onColorSchemeMessage(message: MessageCStoBG, sender: chrome.runtime.MessageSender) { |
| 303 | ASSERT('TabManager.onColorSchemeMessage is set', () => Boolean(TabManager.onColorSchemeChange)); |
no test coverage detected