Automate login to FileMaker Admin Console in web viewer

Today a client asked how to automate login to the admin console when it is open in a web viewer within a FileMaker solution. You probably saw the video Automate web viewer already?

The login here is a bit more difficult as you need a couple of JavaScript calls to login, but here is a script that works for us:

Set Variable [ $JS ; Value: "
function SendEvent(o, name)
{
// send event to JavaScript event listeners¶
var evt = document.createEvent('Events');
evt.initEvent(name, true, true);
o.dispatchEvent(evt);
}

// set user name¶
o = document.getElementById('inputUserName');
o.focus();
o.value = '" & Admin Console::Username & "';
SendEvent(o, 'input');
SendEvent(o, 'blur');

// set password¶
o = document.getElementById('inputPassword');
o.focus();
o.value = '" & Admin Console::Password & "';
SendEvent(o, 'input');
SendEvent(o, 'blur');

document.getElementById('LOGN_Butn_SignIn').click();" ]
Set Variable [ $r ; Value: MBS("WebView.RunJavaScript"; "web"; Substitute ( $JS; ¶; Char(10) )) ]
As you see we define an utility function in JavaScript to trigger an event programmatically. This is required as the login form has event listeners in JavaScript which validate the fields and we need those to enable the Sign In button for us. we do the following steps for each field. We query the field by its ID, set the focus, enter the text and trigger input event to have the JavaScript recognize the changes and then blur it to remove focus. Finally on the end we trigger a click on the login button.

Please note that for text in a formula FileMaker removes the line breaks, so for each comment we add a ¶ character to indicate we want a line break there. Also we do a replace all here to change line endings to unix line endings before we run it with the WebView.RunJavaScript function.


If you have questions, please don't hesitate to contact us. The example will be included with next plugin version.

1 Like

Hello @MonkeybreadSoftware,

Just a comment/tip about snippets such as the one I quoted above:

I almost always include an extra step of escaping strings sourced from FileMaker which are to wind up as hard-coded strings in JavaScript code.

My standard MO is to escape the following chars:

Backslash --> Double Backslash
SingleQuote --> Backslash SingleQuote
Tab --> Backslash t
Return -> Backslash r
Line Feed -> Backslash n

So - somewhere in my FMP code, there would be something that performs a calc similar to:

Let([

  _BS = "\\";
  _SQ = "'"  
];

  Substitute( rawStringValue ;

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

  )
)

I should note that my choice to escape single-quote chars and not double-quote chars is because I always use single-quotes to enclose literal strings in my JS code. For anyone who uses double-quotes to enclose strings, of course, the above would need to be adapted.

If I can avoid using hard-coded strings in the first place, and have the values be passed into the script, or obtained some other way, I usually prefer that, but even so, sometimes there's a situation where it just seems easiest to use some hard-coded values, and when that is the case I'll use the above escaping.

HTH & sincerely,

-steve

1 Like