BE_EvaluateJavascript: SyntaxError: unterminated statement (line 10)

I'm trying to run Javascript through the BE_EvaluateJavascript function in FM18 to produce a registration code.

My code cues the error 'SyntaxError: unterminated statement (line 10)'.

I applied the following Substitute suggestion from another post to fix the issue:

Let([

~input = JavaScript_Code_original ;

_BS = "\" ;
_SQ = "'" // Single quote

];

Substitute( ~input ;
[ _BS ; _BS & _BS ];
[ Char( 10 ); _BS & "n" ];
[ Char( 13 ); _BS & "r" ];
[ Char( 9 ); _BS & "t" ];
[ _SQ ; _BS & _SQ ]
)
)

This produces the error 'SyntaxError: invalid escape (line 1)' (because the Javascript has been turned into a single screed...?).

Here is the Javascript I am trying to process:

const solution_key = '" & $solution_key & "';
const reg_name = '" & $username & "';
const users = " & $users & ";

function generate_registration_code(reg_name, users) {
if (!reg_name || !solution_key) return false;
if (users > 0) reg_name += users;

const len = reg_name.length;
let chr = 0, pos = 0;
for (let loop = 0; loop < len; loop++) {
    const ascii = reg_name.charCodeAt(loop);
    chr += ascii;
    pos += ascii * (loop + 1);
}
const sum = chr + pos + len;
const num_string = sum.toString() + pos.toString() + chr.toString() + len.toString() + (sum * sum).toString();

let code_pre_formatted = '', loop = 0;
while (loop < num_string.length) {
    const segment = parseInt(num_string.substr(loop, 2), 10);
    const convert_num = segment > 56 ? parseInt(num_string.charAt(loop), 10) : segment;
    if (convert_num === 0) {
        code_pre_formatted += solution_key.charAt(0) + solution_key.charAt(1);
    } else {
        code_pre_formatted += solution_key.charAt(convert_num);
    }
    loop += convert_num.toString().length;
}

let code_formatted = '';
for (let i = 0; i < code_pre_formatted.length; i += 4) {
    code_formatted += (i > 0 ? '-' : '') + code_pre_formatted.substr(i, 4);
}
if (users > 0) code_formatted += '-[' + users + ']';

return code_formatted;

}

generate_registration_code(reg_name, users);

Regards,

Morley

I would do this - run this in the FileMaker debugger tool, get a copy of the final javascript string after all the substitutions are done.

Paste that into a text file (naming the file with the .html extension), and wrap the javascript with script tags:

<script>
[... your javascript  ...]
</script>

And then open that file in your browser, running in the browser Developer mode.

That will show you exactly what the problem(s) are.

Here's an example of how a badly formatted script would look in Safari:

1 Like

This looks a lot like code that I have used in the past. The key to understand about it is that this code is not to be used on your entire chunk of JavaScript code. Rather, it is to be used around each individual $variable that one embeds into the JS code.

So, to elaborate: Supposing that you have a CF that does the above solution, and that the CF is named something like EscapeStringForJS.

In that case, the top few lines of your code would look more like this:

const solution_key = '" & EscapeStringForJS( $solution_key ) & "';
const reg_name = '" & EscapeStringForJS( $username ) & "';
const users = " & $users & ";

I'm not going to claim that this will resolve what you are encountering, but it probably is good to sort out that understanding.

Also, a definite +1 to @xochi 's suggestion for troubleshooting. It's a great way to get to the bottom of things.

Another thing to check:

I'd suggest doing a simple test to confirm that the version of BE that you are using uses a JavaScript interpreter that supports the let feature.

Maybe just try executing a simple script, such as:

let a = 'Hello' ;

and see what happens, versus:

var a = 'Hello' ;

I suggest this because different plugins, at different times, had different vintages of JS interpreters, and I kind of recall that there was one plugin which used an old enough interpreter such that I could not use let. It was probably in the vintage of FMP (v18) that you are using.

The other reason that this is occurring to me is that at approximately line 10 of the code posted above is the first occurrence that I see of let being used within the code.

I'd like to thank both of you for taking the time to respond to my query.

With your help, I realised I needed to pass FM field entries through my Javascript as values rather than variables.

Now, the registration code generated on my website may be discretely authenticated in my offline solution.

Regards,

m

2 Likes

Apologies.

I forgot to mention that I couldn't get 'let' to work, so amended the code as follows:

var solution_key = "tcuXd6A4WQTrPL19YkiwNBnxv87MEgDqpfJF52jU3aCSysHzbGKRVZehm";

var reg_name ="Han Solo";

var users =1;

function generate_registration_code(reg_name, users) {

if (!reg_name || !solution_key) return false;

if (users > 0) reg_name += users;

var len = reg_name.length;

var chr = 0, pos = 0;

for (var loop = 0; loop < len; loop++) {

var ascii = reg_name.charCodeAt(loop);

chr += ascii;

pos += ascii * (loop + 1);

}

var sum = chr + pos + len;

var num_string = sum.toString() + pos.toString() + chr.toString() + len.toString() + (sum * sum).toString();

var code_pre_formatted = '', loop = 0;

while (loop < num_string.length) {

var segment = parseInt(num_string.substr(loop, 2), 10);

var convert_num = segment > 56 ? parseInt(num_string.charAt(loop), 10) : segment;

if (convert_num === 0) {

  code_pre_formatted += solution_key.charAt(0) + solution_key.charAt(1);

} else {

  code_pre_formatted += solution_key.charAt(convert_num);

}

loop += convert_num.toString().length;

}

var code_formatted = '';

for (var i = 0; i < code_pre_formatted.length; i += 4) {

code_formatted += (i > 0 ? '-' : '') + code_pre_formatted.substr(i, 4);

}

if (users > 0) code_formatted += '-[' + users + ']';

return code_formatted;

}

generate_registration_code(reg_name, users);

Regards,

M

Thanks for mentioning that. I thought I remembered having been up against that at one point. And it's both nice to know that some of my memory still works, and even more importantly, it's great that someone can read this post and find that out the less hard way.