Skip to content

Instantly share code, notes, and snippets.

@EduardoAC
Last active November 5, 2023 09:46
Show Gist options
  • Save EduardoAC/4da531a11dbd432e3935a2b5b2605868 to your computer and use it in GitHub Desktop.
Save EduardoAC/4da531a11dbd432e3935a2b5b2605868 to your computer and use it in GitHub Desktop.
Chrome Extension - Runtime onMessage Listener, meticulously crafted for capturing messages exchanged between content and background scripts in the context of Manifest v3.
// Message and response are define in https://gist.github.com/EduardoAC/000b1e39a6ec10a892e7c6cd93730a53
chrome.runtime.onMessage.addListener((message: Message<T>, sender, sendResponse) => {
if(sender.tab) { // Sender Tab useful mostly for background script
switch(message.type) {
case "review":
// Tab is useful for instance to obtain the url to fetch the review from
reviewHandler(sender.tab, message, sendResponse)
break
case "language":
languageHandler(sender.tab, message, sendMessage)
break
default:
sendResponse("Error: Not found message type")
break
}
return true // This is really important, tells the extension whether is an ASYNCHRONOUS sendResponse or not
}
return false // False means synchronous response
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment