Tries to initialize all unset settings to their default values when the settings window loads for the first time so that the switches don't return null. However, the initialization trigger I wrote doesn't seem to actually work.
Here's where it's located in the latest experimental version:
And here's my script portion at the top of the settings window. The initialization function is called at the bottom in the window.addEventListener
block.
Basically, it doesn't seem to do anything, making me question if it's actually running or if my script just isn't working properly. But I don't have a good way to test since, well, all my variables are already set.
<script>
var variables = {};
function getAllVariablesAndUpdateClasses() {
console.log('looping through switches');
const buttons = document.querySelectorAll('label');
buttons.forEach(function(button, i) {
const variable = button.id;
window.BTT.callHandler(
'get_string_variable',
{ variableName: variable},
function callback(currentVariableValue) {
console.log(
'the variable',
variable,
'is set to: ',
currentVariableValue
);
variables[button.id] = currentVariableValue;
button.className = currentVariableValue;
}
);
});
}
function trigger_named_async_without_response(actionName) {
window.BTT.callHandler('trigger_named_async_without_response', {trigger_name: actionName},
function callback(scriptResult) {
console.log('action result', scriptResult);
}
);
}
function triggerNamedActionAndRefreshSettingsWindow(actionName) {
trigger_named_async_without_response(actionName);
event.preventDefault();
event.stopPropagation();
setTimeout(function () {
getAllVariablesAndUpdateClasses();
}, 50);
}
function triggerPresetAndRefreshSettingsWindow(actionName) {
trigger_named_async_without_response(actionName);
event.preventDefault();
event.stopPropagation();
setTimeout(function () {
getAllVariablesAndUpdateClasses();
}, 2000);
}
function toggleVariable(variable, actionName, event) {
var button = document.getElementById(variable);
if(variables[variable] === 'true') {
button.className = 'false';
variables[variable] = 'false';
window.BTT.callHandler('set_string_variable', {variableName: variable, to: 'false'});
window.BTT.callHandler(
'get_string_variable',
{ variableName: variable},
function callback(currentVariableValue) {
console.log(
'the variable',
variable,
'is set to: ',
currentVariableValue
);
}
);
} else {
button.className = 'true';
variables[variable] = 'true';
window.BTT.callHandler('set_string_variable', {variableName: variable, to: 'true'});
window.BTT.callHandler(
'get_string_variable',
{ variableName: variable},
function callback(currentVariableValue) {
console.log(
'the variable',
variable,
'is set to: ',
currentVariableValue
);
}
);
}
trigger_named_async_without_response(actionName);
event.preventDefault();
event.stopPropagation();
}
function setVariable(variable, setting, actionName, event) {
window.BTT.callHandler('set_string_variable', {variableName: variable, to: setting});
trigger_named_async_without_response(actionName);
event.preventDefault();
event.stopPropagation();
}
window.addEventListener(
'bttReady',
function(BTT) {
trigger_named_async_without_response('Initialize Settings');
getAllVariablesAndUpdateClasses();
},
false
);
</script>