Under X11 you could use `xprop` to listen for all window activation events and then execute some logic that depends on window class name.
#!/bin/bash
xprop -spy -root _NET_ACTIVE_WINDOW | grep --line-buffered -o '0[xX][a-zA-Z0-9]\{7\}' |
while read -r id; do
class="$(xprop -id $id WM_CLASS)"
if [ -n "$class" ]; then
echo "Active window class is: $class"
fi
done