Allows users to configure scripts
目前為
此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/446506/1491127/%24Config.js
Example instance hosted here. Source code available here.
// Request a config
const $config = new $Config(
'Example Title',
'STORAGE_KEY',
{
'children': [
{'label': 'node0'},
{'label': 'node1'},
{'label': 'node2'}
]
},
([node0, node1, node2]) => ({
value0: node0.value,
value1: node1.value,
value2: node2.value,
}),
);
// Await config load & handle problems
try {
await $config.ready();
} catch (error) {
if (!$config.reset) {
// There's a problem with the arguments passed to the $Config constructor
throw error;
}
// There's a problem with the user's data (could be from manual editing or a script update)
if (!window.confirm(`${error.message}\n\nWould you like to erase your data?`)) {
return;
}
$config.reset();
}
// Apply the user's config
function useConfig() {
const {value0, value1, value2} = $config.get();
// Use the config...
}
useConfig();
// Set up config editing
const button = document.createElement('button');
button.addEventListener('click', async () => {
await $config.edit();
useConfig();
});
Constructor arguments (* = optional):
See the following scripts for practical examples: