Welcome to the Userscript Bridge Library! This library facilitates communication between userscripts running on the same page using the BroadcastChannel
API. Whether you need to request information about installed userscripts or broadcast details about your own script, this library provides a simple interface for achieving that.
This library consists of two main classes:
Script
: Handles sending information about the userscript and listening for messages.Manager
: Requests and processes information from other userscripts.
Additionally, utility functions are provided to build messages and retrieve script info.
Simply include the library in your userscript. For example:
// @require https://github.com/BBBaden-Moodle-userscripts/UserscriptBridgeLib/raw/main/userscriptBridge.lib.js
The Script
class is used to send details about your script and listen for incoming messages.
const connection = new Script();
// When needed, close the connection
// connection.close();
The Manager
class is used to request and fetch information about installed userscripts.
const connection = new Manager();
connection.fetchInstalledUserscripts().then((userscripts) => {
console.log('Installed Userscripts:', userscripts);
});
sendInfo()
: Sends details about the current userscript.close()
: Closes the BroadcastChannel.
fetchInstalledUserscripts()
: Requests and retrieves details about installed userscripts.close()
: Closes the BroadcastChannel.
Builds a message to be sent via BroadcastChannel.
Parameters:
type
- Type of the message.data
- Data to be included in the message.
Returns: A message object.
Retrieves information about the current userscript.
Returns: An object containing script details such as name, namespace, version, etc.
- Ensure your userscripts are running on the same page for them to communicate.
- Use the
Script
class to broadcast information and theManager
class to listen for that information.
This extension was originally made by Black Backdoor.