Touch Bar Button icon based on state

What is the "proper" way to update icons for BetterTouchTool Touch Bar Button based on some external info? For example, I'd like to use a different background color for my "Back" and "Forward" (History) Safari buttons based on whether any of these are available in the app itself. So if there's no available page in history (no "Back" button available, it's greyed out in History -> Back) set different color for it.

@Andreas_Hegenberg any ideas here? I'd also like to have a similar solution for all the back/forward buttons I end up adding to the touch bar. Thanks!

There is usually no way for such state to be "pushed" towards BTT, thus you would need to repeatedly run an Apple Script that checks this state. The BetterTouchTool Apple Script widgets are made for this, from that script you can then return some value and then use the alternate color regex:

(In this example the alternate icon and color would be enabled if the script returns test)

Alternatively a script can also return a image as base64 or the path to an icon, but I'd only recommend that for advanced usecases: