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

How do you escape something like "[[text]]" in sendChat? tilde and /direct don't work

1682182341

Edited 1682183773
I'm trying to send "[[text]]" via sendChat, and not have it processed at all. I've tried variations of "/direct" and "`" from looking here: <a href="https://wiki.roll20.net/API:Chat#sendChat.28speakingAs.2C_input_.5B.2Ccallback_.5B.2C_options.5D.5D_.29_.5BAsynchronous.5D" rel="nofollow">https://wiki.roll20.net/API:Chat#sendChat.28speakingAs.2C_input_.5B.2Ccallback_.5B.2C_options.5D.5D_.29_.5BAsynchronous.5D</a> <a href="https://wiki.roll20.net/Text_Chat#Escape_Text_.28.60.29" rel="nofollow">https://wiki.roll20.net/Text_Chat#Escape_Text_.28.60.29</a> I can't seem to find anything that works, and the sendChat page has no mention of escape characters that I can find. These are some of the variations I've tried: sendChat("Export", "`[[text]]"); sendChat("Export", "/direct [[text]]"); sendChat("Export", "`/direct [[text]]"); sendChat("Export", "/direct `[[text]]"); HTML escapes don't seem to work either. "&amp;amp;" works properly and outputs "&amp;", but " &amp;#5b;" doesn't work properly and is just output as-is. sendChat("Export", "&amp;#5b;&amp;#5b;text&amp;#5d;&amp;#5d;"); Edit: Nevermind, I'm dumb. I forgot HTML escapes aren't hex by default. :) This works fine: sendChat("Export", "&amp;#x5b;&amp;#x5b;text&amp;#x5d;&amp;#x5d;");
1682183866
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
5b is the Unicode hex reference for opening bracket, you need the html entity for it which is either one of &amp;#91: &nbsp;or &amp;#lbrack; .
Thanks! I just realized that now too. That's what I get for not verifying my HTML escape in a separate .html file and assuming everything is hex by default. :)
1682184403

Edited 1682184515
Oh, just to double check since I'm so new to roll20, is this the "preferred" way to do this kind of escaping btw? I thought "/direct" would be what I want originally, so I'm just wondering if I'm missing a better way to handle this than just doing "[" -&gt; "&amp;#x5b" replacement.
1682188607
timmaugh
Forum Champion
API Scripter
HTML replacement is the way to go to get around the order of operations and syntax-identification for Roll20 parsing. Meaning... as an example... if you have nested attributes (AttrA contains a reference to AttrB which contains a reference to AttrC), those will be expanded up to 99 times. Any of them could put some syntax into the command line that would be detectable (like your inline-roll syntax). In order to preserve the text until *after* Roll20 parsers finish their order of ops, you use the HTML replacements. HTML is unescaped at the point of presentation or, for roll queries, once for each level of query prompt... I believe. Someone can correct me if I'm wrong. Double-escaping: [&nbsp; &nbsp; =&gt;&nbsp; &nbsp; &amp;lbrack;&nbsp; &nbsp; =&gt; &amp;amp;lbrack;
Awesome, thanks for the additional info!
1682238428
GiGs
Pro
Sheet Author
API Scripter
I endorse everything said so far, but I have to ask: why are you using [[ ]]? Are there any other characters you can use so you don't need to use those?
It's because I'm trying to export some of the raw HTML for things like GM notes and our campaign journal, so I needed a way to fully escape an arbitrary string I have no control over.
1682273373
The Aaron
Roll20 Production Team
API Scripter
This is the function I use for escaping for Roll20: const HE = (() =&gt; { const esRE = (s) =&gt; s.replace(/(\\|\/|\[|\]|\(|\)|\{|\}|\?|\+|\*|\||\.|\^|\$)/g,'\\$1'); const e = (s) =&gt; `&amp;${s};`; const entities = { '&lt;' : e('lt'), '&gt;' : e('gt'), "'" : e('#39'), '@' : e('#64'), '{' : e('#123'), '|' : e('#124'), '}' : e('#125'), '[' : e('#91'), ']' : e('#93'), '"' : e('quot') }; const re = new RegExp(`(${Object.keys(entities).map(esRE).join('|')})`,'g'); return (s) =&gt; s.replace(re, (c) =&gt; (entities[c] || c) ); })();
Thanks! Look like I'm missing "@" and "|" from the list I have. I was just escaping stuff as it came in and caused the script to error out.
1682273490
The Aaron
Roll20 Production Team
API Scripter
It's another IIFE that returns a function (see other one of your threads). It's important to separate the &amp; and the rest of entity in your script code as the API Sandbox will eat them if you get things in the 1-click.
What do you mean by the "1-click" btw?
1682273764
The Aaron
Roll20 Production Team
API Scripter
Scripts in the Roll20 Official Github can ask to be included in the 1-click Script Library, and can then be installed from the dropdown:
Oh cool, had no idea that was a thing. (I'm still very noob). I'll read up on that!