Give this a try: /* global TokenMod ChatSetAttr */
on('ready',()=>{
const bar = 'bar1';
const upColor = '#0d0';
const downColor = '#f66';
const getActivePages = () => [...new Set([
Campaign().get('playerpageid'),
...Object.values(Campaign().get('playerspecificpages')),
...findObjs({
type: 'player',
online: true
})
.filter((p)=>playerIsGM(p.id))
.map((p)=>p.get('lastpage'))
])
];
const sign = (n) => (n>0 ? '+' : (n<0 ? '-' : ' '));
const colored = (c,p,s) => ((c>p) ? `<span style="color:${upColor}">${s?sign(c):''}${c}</span>` : ((c<p) ? `<span style="color:${downColor}">${c}</span>` : c ));
const handleTokenChange = (obj,prev)=>{
let pages = getActivePages();
if(pages.includes(prev._pageid) ){
let hp = obj.get(`${bar}_value`);
let hpMax = obj.get(`${bar}_max`);
let pHp = prev[`${bar}_value`];
let pHpMax = prev[`${bar}_max`];
let hpDiff = (hp-pHp);
let hpMaxDiff = (hpMax-pHpMax);
if(hp !== pHp || hpMax !== pHpMax){
sendChat('',`/w gm <div style="padding: 2px .5em; font-size: .8em;background: #333;color:#ccc;border-radius:.5em;"><code>${prev.name}</code> ${pHp}/${pHpMax} => ${colored(hp,pHp)}/${colored(hpMax,pHpMax)} (${colored(hpDiff,0,true)}/${colored(hpMaxDiff,0,true)})</div>`);
}
}
};
on('change:graphic', handleTokenChange);
if('undefined' !== typeof TokenMod && TokenMod.ObserveTokenChange){
TokenMod.ObserveTokenChange(handleTokenChange);
}
if('undefined' !== typeof ChatSetAttr && ChatSetAttr.registerObserver){
ChatSetAttr.registerObserver('change',handleTokenChange);
}
});
Here's what it looks like: Also supports TokenMod and ChatSetAttr changes.