Thomas Kratz

NAME

Win32::HTA - HTML Applications with Perl as local backend

SYNOPSIS

    use Win32::HTA;

    my $hta = Win32::HTA->new();

    # Simple MessageBox with return value
    my $chosen = $hta->show(
        TITLE => 'Important Information!',
        DLG_HTML => qq(
            <p id="msg_txt">Something happened!</p><br>
            <button id="bok"     type="button" onclick="ret_handler();">OK</button>
            <button id="bcancel" type="button" onclick="esc_handler();">CANCEL</button>
        ),
        CSS => q(
            p      { width: 250px; text-align: center; }
            button { width:  80px; margin-left: 30px;}
        ),
        ON_LOAD => q[
            bok.focus();
        ],
        RET_HANDLER => q(pipe_string('OK')),
        ESC_HANDLER => q(pipe_string('CANCEL')),
    );
    print $chosen, "\n";

    # Reset to default
    $hta->clear();


    # AJAX communication
    require JSON;
    $hta->show(
        AJAX => sub {
            my($request) = @_;
            print JSON::encode_json($request);
            my $ok = int(rand()+0.5);
            return {
                succeeded => $ok,
                data      => {
                    name => $request->{request},
                    text => $request->{data},
                },
                $ok ? () : ( errtxt => 'Something went wrong!'),
            };
        },
        IE_MODE => 11,
        BG_COLOR => '#888800',
        DLG_HTML => qq(
            <button id="but" type="button" onclick="bpress()">Talk to Perl</button><br>
        ),
        RET_HANDLER => q(but.click();),
        CSS => 'button {width: 150px;}',
        JS_HEAD => q(
            function bpress() {
                var reply = ajax_request({request : "test", data : 'testdata'});
                if ( reply['succeeded'] ) {
                    alert(reply['data']['text']);
                } else {
                    alert(reply['errtxt']);
                }
                close();
            }
        ),
    );

DESCRIPTION

HTML Applications are an easy way to build simple and not so simple GUI applications using HTML with script languages supported by Internet Explorer (Javascript, VBScript and even PerlScript if you are using ActivePerl).

As I am using Strawberry Perl, I thought launching mshta.exe from inside perl and then communicating via AJAX could be interesting.

CONSTRUCTOR

new(%args)

Creates a new Win32::HTA object with the options given. See "OPTIONS"

METHODS

clear(%args)

Resets the object to the default values and sets the given values afterwards. For options see "OPTIONS"

show(%args)

Displays the Application window with mshta.exe. Waits for the application to close. Will return the string written by pipe_string() from the javascript side of the HTA unless the 'AJAX' option is used.

For options see "OPTIONS".

Accessor methods

There are lower case accessor methods for all options. So instead of passing options to new() clear() and show() you can do e.g.:

  $hta->title("Titletext");

  $hta->show();

OPTIONS

AJAX

Reference to a subroutine that acts as a request handler. It will get passed a reference to whatever you request from the javascript side of your HTA. It will be already parsed by JSON::decode_json, so you can access the contents directly.

If you want to return something it has to be a reference to an array or hash. It will be run through JSON::encode_json() and parsed by JSON.parse on the javascript side automatically.

You can use the javascript function 'ajax_request(<obj>)' from inside your javascript blocks given in the options JS_HEAD or JS_BODY. The synopsis has a working example.

BG_COLOR

Shortcut for setting the background color for the HTML <body> tag.

CSS

Verbatim style sheet block. It will be set inside the <head> tag.

DEBUG

If set to a true value will output the generated HTML to stderr.

DLG_HTML

The HTML text for the interface. It will be placed inside a div with id '__DIV__' that is a direct child of <body>

ESC_HANDLER

Javascript code that gets called when <Escape> is pressed. The event is bound to the HTML <body>. There is a corresponding javascript funtion called 'esc_handler(obj)' that will be called

H_ADJUST

Number of pixels to adjust the height of the main window to account for window decorations. Defaults to 44.

additional tags place at the beginning of the header (before JS_HEAD)

IE_MODE

Internet Explorer version that will be emulated by mshta.exe. Defaults to 8. This could be important especially for javascript capabilities.

IP_PORT

The tcp/ip port that will be used for listening to ajax requests. The default is 0, which will let the OS chose the port. This is the safe option. Use at your own risk.

JS_BODY

Javascript code that gets inserted after the main <div> tag.

JS_HEAD

Javascript code that gets inserted inside the <head> tag.

ON_LOAD

Javascript code that gets inserted inside a window.onload handler.

POST_HANDLER

Reference to a subroutine that will be called with expanded HTML text as only parameter after variable expansion. Has to return the HTML text. Use at your own risk.

PRE_HANDLER

Reference to a subroutine that will be called with the HTML template text as only parameter before variable expansion. Has to return the new template text. Use at your own risk.

RET_HANDLER

Javascript code that gets called when <Return> is pressed. The event is bound to the HTML <body>.

TITLE

Text for the <title> tag.

W_ADJUST

Number of pixels to adjust the width of the main window to account for window decorations. Defaults to 24.

JAVASCRIPT FUNCTIONS

There are some predefined utility functions you can use in your javascript code:

dump_obj(obj)

Use this for dumping object information if your choosen IE Version does not contain the better JSON.stringify(obj).

add_event(obj, type, function)

Wrapper for obj.addEventListener() or obj.attachEvent() that should do the right thing for the different IE versions.

pipe_string(str)

Writes the string to stdout and closes the HTA. Will only work when you don't use AJAX for communication.

ajax_request(obj)

Sends the stringified object data as AJAX request to the perl scripts callback that has been specified with the AJAX option.

AUTHOR

Thomas Kratz <tomk@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2016 by Thomas Kratz

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.