Skip to content

Toolbox Module Structure

Erin edited this page May 3, 2022 · 7 revisions

This document aims to give a global overview of the toolbox module structure. Refer to the Module class source code in tbmodule.js for more information.

Modules are defined in individual files by constructing and exporting an instance of Module, a named export from tbmodule.js. They are then imported and registered in init.js after startup checks are performed. Helper functions, constants, etc. can be defined at the top level, and helpers can also be exported to facilitate interoperation between modules if desired.

Example empty module

// modules/mymodule.js
import {Module} from '../tbmodule.js';

// The actual module should be the default export
export default new Module({
    name: 'My Module', // Module name displayed in settings UI
    id: 'MyModule', // Internal module ID used in log messages and storage keys
    enabledByDefault: true, // Enables module on fresh installs
    settings: [
        {
            id: 'thisIsABoolean',
            description: 'The text accompanying the setting control in the settings page',
            type: 'boolean',
            default: false,
            advanced: true,
        },
        {
            id: 'thisIsASelector',
            description: 'This is a selector setting',
            type: 'selector',
            values: ['value', 'Other value', 'Third value'],
            default: 'value',
        },
    ],
}, async function ({thisIsABoolean, thisIsASelector}) {
    // Module initializer - run as soon as possible if the module is enabled
    // If not using an arrow function, `this` is bound to the module instance
    // thisIsABoolean and thisIsASelector refer to the current values of the settings with those IDs

    // Module instances have logging utilities built in:
    this.success('Hello from the new module!');

    // Update a setting
    await this.set('thisIsASelector', 'Other value');
});