plugin.settings()
Provides access to the settings API. Your plugin gets its own page in the UnderScript settings window, under Plugins.
Syntax
plugin.settings();
- Return Value
- A
Settingsobject
Methods
add()
Add a setting to your plugin’s page.
settings.add({
key, name, type, default, note, description, category, refresh, disabled, hidden, remove, reset, export, extraPrefix, data, transform, onChange, converter,
});
- key:
string - Setting key, required. UnderScript adds your plugin’s name to the front, so keys never clash with other plugins.
- name:
stringorfunction(): string - Name shown in the settings window (default: the key)
- type:
string,objector a custom type - Type of setting (default:
boolean).
See Setting Types - default:
anyorfunction(): any - Default setting value
- note:
stringorfunction(): string - Shown when hovering over the setting
- description:
stringorfunction(): string - Longer text shown under the setting
- category:
string - Groups the setting under a titled box on your plugin’s page. Settings with the same category share a box.
- refresh:
booleanorfunction(): boolean - True to append “Will require you to refresh the page” to your setting’s note
- disabled:
booleanorfunction(): boolean - True to disable the setting
- hidden:
booleanorfunction(): boolean - True to register the setting without showing it in the settings window
- remove:
booleanorfunction(): boolean - For type
boolean. True to delete the stored value when the box is unchecked, instead of storing0 - reset:
booleanorfunction(): boolean - Adds a reset button (sets to default)
- export:
booleanorfunction(): boolean - False to leave the setting out of exports (default: true)
- extraPrefix:
string - Exports also include every stored key that starts with this prefix. For data you keep in
localStorageoutside of settings, but want to associate with the setting. - data:
anyorfunction(): any - Extra data for the setting type. What it should be depends on the type, see Setting Types. Custom types from addType() read it too.
- transform:
function(value): any - Changes the value before you receive it, for the default value too. The stored value is not changed.
value(),on()andonChangeall get the transformed value. - onChange:
function(newValue, oldValue): void - Called when the setting value is changed
- converter:
function(stored): any - Runs once when the setting is registered, and only if a value is stored. Receives the stored string.
Return a new value to store,nullto remove the stored value, orundefinedto leave it as is. Useful for migrating values from an older format. - Return Value
- A
Settingobject
Setting {
key, // the full key
disabled, // true if currently disabled
value(), // get the current value
set(value), // change the value
on(fn), // fn(newValue, oldValue) when it changes
show(), // open the settings window on this setting
refresh(), // refresh the setting after changing what it depends on
}page()
Add a setting to a different tab of your plugin’s page.
settings.page(name);
- Return Value
- An object with
add()(the same as above) andopen(). The tab shows up next to your plugin’s first tab.
name()
Rename the first tab of your plugin’s page. Returns true if it was renamed.
settings.name(name);
value()
Get the value of any setting by its full key.
settings.value(fullKey);
on()
Listen for setting events.
settings.on(event, listener);
setting:change(key, value, previous),open,close- Events for the whole window. You can also pass the full key of a setting to listen to just that setting.
open()
Open the settings window to your plugin’s page.
settings.open();
isOpen()
Returns true if the settings window is open.
settings.isOpen();
addType()
Register your own type of setting, made with underscript.utils.SettingType. Its name gets your plugin name added to the front, plugin name:type name.
settings.addType(type);
A type needs element(), which builds what the player interacts with, and value(), which turns the stored string into the value your plugin receives. You can also override default(), encode(), styles() and labelFirst().
- element(value, update, info)
valueis the current value. Callupdate(newValue)to change it, orupdate(undefined)to delete it.
infohasdata(the setting’sdata),key(the full key),name,disabled,remove,container(an element placed under the setting that you can fill), andremoveSetting()(deletes the setting completely).- value(stored, data)
- Return the value type this setting is meant to have.
Setting Types
- boolean
- Checkbox (true/false) (default).
data:{ reverse, extraValue }.reverseflips the value, so unchecked istrue.extraValueis one more stored value to count astrue. - select
- Drop down menu.
data: the items to choose from, each a value or[label, value], like['Low', ['High', 'h']] - text
- A text box
- password
- A text box that hides what you type
- color
- Color selector
- slider
- A sliding bar.
data:{ min, max, step }(defaults 0, 100 and 1) - keybind
- Click and press a key or mouse button.
data:{ labels }, an object of key names to the text shown for them - array
- A list of text you can add to and remove from. Type in the box and press Enter to add an item, and click an item to remove it. The value is an array of strings.
- list
- The items in
data, which the player drags into the order they want.
data: the items, each a string or an object{ label, value, class }.labelis the text shown (textalso works),valueis what gets stored (default: the label), andclassadds CSS classes to the item.value()gives back the items themselves, in order, so an object item comes back as an object. Items you add todatalater go to the end of an existing order. - map
- Pairs of keys and values
- { key, value }
- Pass an object as the
typefor a map with typed keys and values, liketype: { key: 'text', value: 'color' }.keydefaults totext, andvaluedefaults to the type ofkey. - remove
- Deletes itself when selected
- hr
- A divider line
Examples
const plugin = underscript.plugin('My great plugin');
const settings = plugin.settings();
const enabled = settings.add({
key: 'enabled',
name: 'Enable my feature',
description: 'Turns on the feature that does the thing',
default: true,
onChange(value) {
console.log('Now', value);
},
});
if (enabled.value()) {
// Do the thing
}
// A second tab
settings.page('Advanced').add({
key: 'delay',
name: 'Delay',
type: 'slider',
data: { min: 0, max: 10 },
default: 2,
});Migrating a stored value to a new format
const delay = settings.add({
key: 'delay',
name: 'Delay (seconds)',
type: 'slider',
data: { min: 0, max: 10 },
default: 2,
converter(stored) {
if (!stored.endsWith('s')) return undefined;
return Number(stored.slice(0, -1));
},
});A custom type
class Stars extends underscript.utils.SettingType {
constructor() {
super('stars');
}
element(value, update, { data = 5 }) {
return $('<input type="number">')
.attr({ min: 1, max: data })
.val(value)
.on('change', (e) => update(e.target.value));
}
value(stored) {
return Number(stored);
}
default() {
return 1;
}
}
const stars = new Stars();
settings.addType(stars);
settings.add({
key: 'rating',
name: 'Rating',
type: stars,
data: 10,
});