Skip to content

Instantly share code, notes, and snippets.

@shdwjk
Forked from keithcurtis1/gmnote.js
Last active January 20, 2019 15:00
Show Gist options
  • Save shdwjk/7844e8340f305360959e58803bb208ba to your computer and use it in GitHub Desktop.
Save shdwjk/7844e8340f305360959e58803bb208ba to your computer and use it in GitHub Desktop.
This pulls the GM notes from a token on Roll20 and whispers them to the chat
on('ready',()=>{
const blockElements = [
'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'pre', 'address',
'blockquote', 'dl', 'div', 'fieldset', 'form', 'hr', 'noscript', 'table','br'
];
const rStart=new RegExp(`<\\s*(?:${blockElements.join('|')})\\b[^>]*>`,'ig');
const rEnd=new RegExp(`<\\s*\\/\\s*(?:${blockElements.join('|')})\\b[^>]*>`,'ig');
const getLines = (str) =>
(rStart.test(str)
? str
.replace(/[\n\r]+/g,' ')
.replace(rStart,"\r$&")
.replace(rEnd,"$&\r")
.split(/[\n\r]+/)
: str
.split(/(?:[\n\r]+|<br\/?>)/)
)
.map((s)=>s.trim())
.filter((s)=>s.length)
;
const cmdRegex = /^!(w?)gmnote(?:-(.*))?(.*)$/i;
on('chat:message',(msg) => {
if('api' === msg.type && cmdRegex.test(msg.content) && playerIsGM(msg.playerid) ){
let parts = msg.content.split(/\s+--ids\s+/i);
let match=parts[0].match(cmdRegex);
let output = match[1].length ? '/w gm ' : '';
let regex;
let ids = (msg.selected ? msg.selected.map((o)=>o._id) : []);
if(parts[1]){
ids = [...ids, ...parts[1].split(/\s+/)];
}
if(match[2]){
regex = new RegExp(`^${match[2]}`,'i');
}
ids.map( id => getObj('graphic',id))
.filter( o =>undefined !== o && o.get('gmnotes').length > 0 )
.forEach( o => {
log(o);
if(regex){
let lines = _.filter(
getLines(unescape(o.get('gmnotes'))),
(l) => regex.test(l.replace(/<[^>]*>/g,''))
).join('<br>');
sendChat(o.get('name'), `${output}${lines}`);
} else {
sendChat(o.get('name'), `${output} &{template:spell}}{{name=${unescape(o.get('gmnotes')).replace(/(?:[\n\r]+|<br\/?>)/g,'<br>')} }}`);
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment