Hi there!
I'm trying to create a custom menubar item that uses Javascript to change its appearance when a certain condition is met. Ideally it would run automatically every couple minutes. Now, I have written the script and when I test it using the "Run" option in the BTT editor, it works as expected. However, when I try to use it in a "Custom Menubar Item" trigger with the "Run Real Javascript" action or with a "Javascript Menubar Item" trigger and set it to run automatically, it does not work properly.
In both cases, it seems to execute the script and changes the appearance. Unfortunately the desired background color only shows up for a split second and then is reset to default (none). This does not happen when I use the "Run" option. In fact, I think the automated execution often doesn't trigger at all after some more conscious observing.
I use a SF symbol as icon and hide the text, just to provide some additional info.
Any ideas?
Here's a simplified version of the script. I think it should be fine.
(async ()=> {
let uuid = "3BF4C33E-8B7C-4AD9-B5EB-8155B86E5C66";
// Define states
var offJSON = {uuid: uuid,
"background_color": "200,50,100,255"
};
var onJSON = {uuid: uuid,
"background_color": "50,200,100,255"
};
var warnJSON = {uuid: uuid,
"background_color": "250,200,100,255"
};
// Check data
async function check(headers) {
let r = await fetch('x', { method: 'GET', credentials: 'include', headers: headers });
let html = await r.text();
return html.includes('text');
};
let bool1 = await check(headers1);
let bool2 = await check(headers2);
// Change menu bar
if (bool1 === false && bool2 === false) {
await callBTT('update_menubar_item', offJSON);
} else if (bool1 === true || bool2 === true) {
await callBTT('update_menubar_item', onJSON);
} else {
await callBTT('update_menubar_item', warnJSON);
};
returnToBTT('Done');
})();
Cheers!