Back to Content

runtime.onUserScriptConnect

files/en-us/mozilla/add-ons/webextensions/api/runtime/onuserscriptconnect/index.md

latest1.5 KB
Original Source

Fired when a connection is made with a user script 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 connect and then sent messages from a USER_SCRIPT world that is configured by {{WebExtAPIRef('userScripts.configureWorld()')}} with messaging set to true.

Syntax

js-nolint
browser.runtime.onUserScriptConnect.addListener(listener)
browser.runtime.onUserScriptConnect.removeListener(listener)
browser.runtime.onUserScriptConnect.hasListener(listener)

Events have three functions:

  • addListener(listener)
    • : Adds a listener to this event.
  • removeListener(listener)
    • : Stop listening to this event. The listener argument is the listener to remove.
  • hasListener(listener)
    • : Checks whether a listener is registered for this event. Returns true if it is listening, false otherwise.

addListener syntax

Parameters

  • function
    • : The function called when this event occurs. The function is passed this argument:
      • port
        • : {{WebExtAPIRef('runtime.Port')}}. An object connecting the current script to the other context.

{{WebExtExamples("h2")}}

Browser compatibility

{{Compat}}