Skip to content

Instantly share code, notes, and snippets.

@EduardoAC
Last active June 7, 2024 12:08
Show Gist options
  • Save EduardoAC/eeb457de36941e2e4bc04e807b766a20 to your computer and use it in GitHub Desktop.
Save EduardoAC/eeb457de36941e2e4bc04e807b766a20 to your computer and use it in GitHub Desktop.
Chrome Extension - Site Page Review - Message Handler. The goal of this function is to orchestrate the messages received by the content scripts and proxy them to the right handler.
import { MessageRate } from "../../types/message.types"
import { ratingMessageHandler } from "./onMessageHandlers/ratingMessageHandler"
export function messageHandler(
message: Message,
sender: chrome.runtime.MessageSender,
sendResponse: SendResponseCallback,
) {
if (sender.tab && message.type) {
switch (message.type) {
case "rating":
ratingMessageHandler(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
message as any as Message<MessageRate>,
sendResponse,
)
break
default:
return false
}
return true // We will process the messages asynchronously
}
return false
}
@EduardoAC
Copy link
Author

The project example of a site page review can be found at https://github.com/EduardoAC/site-review-extension.

Also, the code for the message sender can be found at https://gist.github.com/EduardoAC/000b1e39a6ec10a892e7c6cd93730a53.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment