Skip to content

Instantly share code, notes, and snippets.

@Moizsohail
Created March 3, 2022 08:28
Show Gist options
  • Save Moizsohail/9086a78918623866011149c8624777ec to your computer and use it in GitHub Desktop.
Save Moizsohail/9086a78918623866011149c8624777ec to your computer and use it in GitHub Desktop.
Chrome Extension In React With NPM Modules: Part 2
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