Created
March 3, 2022 08:28
-
-
Save Moizsohail/9086a78918623866011149c8624777ec to your computer and use it in GitHub Desktop.
Chrome Extension In React With NPM Modules: Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MessageTypes } from "./types"; | |
export const sendMessage = ( | |
messageType: MessageTypes, | |
payload?: object | null, | |
callback?: any | |
) => { | |
if (!payload) payload = {}; | |
chrome.windows.getCurrent((w) => { | |
chrome.tabs && | |
chrome.tabs.query({ active: true, windowId: w.id }, (tabs) => { | |
if (tabs.length === 0) { | |
return; | |
} | |
chrome.tabs.sendMessage( | |
tabs[0].id as number, | |
{ type: messageType, ...payload }, | |
callback | |
); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment