I personally use a lot of roll queries and queries with roll templates as outputs which can be a little frustrating due to having to convert all the HTML entities and remember what level of nesting I'm on. I thought there's probably a really simple way to speed up the process with the API, so simple that even I could do it. So here it is, unfortunately, as I'm not a Pro subscriber, I don't have direct access to the API which is why I'm asking anyone who feels up to it to test out this script and essentially tell me if it works. on("chat:message", function(msg) {
if(msg.type == "api" && msg.content.indexOf(“!query ") !== -1) {
var level=0;
var open=0;
var isAttr=0;
var attrOpen=0;
var str = ""
var i;
for (i=0;i<msg.length;i++) {
var char = msg.charAt(i);
if (char=="?") {level++; str=str+char}
else if (char=="@" && level>0) {isAttr++; str=str+char;}
else if (char=="%" && level>0) {isAttr++; str=str+char;}
else if (char=="{" && level>0 && isAttr==0) {open++; str=str+char;}
else if (char=="{" && level>0) {attrOpen++; str=str+char;}
else if (char=="}" && isAttr>0) {attrOpen--; str=str+char;
if (attrOpen==0) {isAttr--;} }
else if (char=="}" && open==level) {level--;open--;
if (level>0) {str=str+"&"+"amp;".repeat(level-1)+"#125;";}
else {str=str+char} }
else if (char=="}" && open>level) {open--;
if (level>0) {str=str+"&"+"amp;".repeat(level-1)+"#125;";}
else {str=str+char} }
else if (char=="|" && isAttr>0) {str=str+char;}
else if (char=="|" && level>1) {str=str+"&"+"amp;".repeat(level-2)+"#124;";}
else if (char=="," && level>1) {str=str+"&"+"amp;".repeat(level-2)+"#44;";}
else {str=str+char;}
}
str=str.replace(“!query”,””)
sendChat(msg.who,str);
}
});
It should convert } | and , to their appropriate HTML entities depending on the level of nesting and convert } where it's not the end of a query, e.g. with the {{}} formatting of roll templates. Unfortunately, I can't see any way to do the same with | and , as there is no way the code could work out which is part of the query syntax and which is part of an entry into the query itself. It will also not change | and } inside attribute and ability calls as they are called before queries and don't need to be converted to entities. Thanks to anyone who tests the script and I appreciate any feedback or requests for the script.