Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

First Script Needs Testing - AdvQuery

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.
1513442916
GiGs
Pro
Sheet Author
API Scripter
I havent tested your script (I dont have any macros with the relevant construction to test this with), but I'm a little confused. This seems more than a little convoluted, so I'm probably missing something. If you don't have access to the API, how do you intend to use the script? And conversely, if you do have access to the API, can't you avoid the need to use these replacement functions, and just write your macros as API scripts instead?
1513447322
Jakob
Sheet Author
API Scripter
This is just transforming some text into some other text, in a convoluted way, no? Why do you need the API for that, you can just do that in a script locally, with the added benefit that you don't need to have API access to use it?
1513458786
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yep, personally I use a google sheet using pattern matching to replace problem characters.
Nice first script!
1513461889
vÍnce
Pro
Sheet Author
It's really too bad that roll20 doesn't have a method to "parse..?" certain HTML characters within queries automatically.  Not sure how difficult it would be to implement but it would sure make nested queries sooooooooooo much easier to create and share.  
1513466488
Ada L.
Marketplace Creator
Sheet Author
API Scripter
It took me some studying to figure out what you're trying to do with your script. I noticed one error in it almost immediately on the line: var char = msg.charAt(i); You should use msg.content.charAt here instead of msg.charAt. I would also really highly recommend committing to a consistent indentation/bracket style for your code. It's hard to read and could use a lot of cleaning up.
1513475392
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Scott C. said: Yep, personally I use a google sheet using pattern matching to replace problem characters. Any chance you could share....? :)
1513483830
Ada L.
Marketplace Creator
Sheet Author
API Scripter
Here's another handy tool for replacing special characters using regular expressions:&nbsp;<a href="https://regex101.com/" rel="nofollow">https://regex101.com/</a>
1513488769
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
keithcurtis said: Scott C. said: Yep, personally I use a google sheet using pattern matching to replace problem characters. Any chance you could share....? :) Yep, I've shared it in a few threads. Let me dig it back out (I haven't had to do any query nesting in a while)