Just out of curiosity, are the newlines between command strings generated by get_js_commands required? It appears that when the final string is passed into json_serialize they're turned back into '\n' before being sent which seems to make them somewhat unnecessary. They might as well just be a single space, or even nothing if you want to be efficient.
-Brad
List info: http://lists.roundcube.net/dev/ BT/aba52c80