files/en-us/mozilla/add-ons/webextensions/api/runtime/onuserscriptmessage/index.md
Use this event to listen for messages sent from one of the extension's USER_SCRIPT worlds.
In Firefox, this event requires the userScripts permission. In Chrome, the event is always present, including in extensions that don't declare the userScripts permission.
A user script can only send messages using {{WebExtAPIRef('runtime.sendMessage')}} from a USER_SCRIPT world that is configured by {{WebExtAPIRef('userScripts.configureWorld()')}} with messaging set to true.
Along with the message, the listener is passed:
sender object with details about the message sender.sendResponse function the listener can use to send a response back to the sender.browser.runtime.onUserScriptMessage.addListener(listener)
browser.runtime.onUserScriptMessage.removeListener(listener)
browser.runtime.onUserScriptMessage.hasListener(listener)
Events have three functions:
addListener(listener)
removeListener(listener)
listener argument is the listener to remove.hasListener(listener)
listener is registered for this event. Returns true if it is listening, false otherwise.listener
message
object. The message. This is a JSON-ifiable object.sender
sendResponse
: A function to call, at most once, to send a response to the message. The function takes one argument, which is any JSON-ifiable object. This argument is passed back to the message sender.
If you have more than one onUserScriptMessage listener in the same document, then only one can send a response.
To send a response synchronously, call sendResponse before the listener function returns. To send a response asynchronously, do one of these:
sendResponse argument and return true from the listener function. You can then call sendResponse after the listener function has returned.Promise from the listener function and resolve the promise when the response is ready.In this example, a user script in a USER_SCRIPT world with the ID myScriptWorld sends a message to the extension that registered it:
// The user script
// Send a message to the extension that registered the user script
browser.runtime.sendMessage("my message");
// The extension that registered the user script
function handleMessage(message, sender) {
// check that the message originated from "myScriptWorld" world
if (sender.userScriptWorldId === "myScriptWorld") {
// process message
console.log(message);
}
}
browser.runtime.onUserScriptMessage.addListener(handleMessage);
{{WebExtExamples}}
{{Compat}}