Advertisement
Guest User

Command line for HTA

a guest
Mar 31st, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 9.97 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <!-- Copyright (c) 2013 Alf P. Steinbach -->
  4.         <!-- How to retrieve a command line for an IE10-based Windows HTML-Application (HTA). -->
  5.  
  6.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  7.         <!-- A windows web control defaults to quirky IE7 semantics. Request for better: -->
  8.         <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9.         <meta http-equiv="MSThemeCompatible" content="yes">
  10.  
  11.         <title>Command line</title>
  12.  
  13.         <style>
  14.             * { font: 10pt 'MS Shell Dlg 2'; }
  15.  
  16.             #command-line {
  17.                 background-color: lightBlue;
  18.             }
  19.         </style>
  20.  
  21.         <script type="text/javascript">
  22.             function is_whitespace( s )
  23.             {
  24.                 var n = s.length;
  25.                 if( n == 1 )
  26.                 {
  27.                     return (s == ' ' || s == '\n' || s == '\r' || s == '\t');
  28.                 }
  29.                 else
  30.                 {
  31.                     for( var i = 0; i < n; ++i )
  32.                    {
  33.                        if( !is_whitespace( s.substr( i, 1 ) ) )
  34.                        {
  35.                            return false;
  36.                        }
  37.                    }
  38.                    return true;
  39.                }
  40.            }
  41.  
  42.            // This function doesn't work when using IE10 engine or better, because
  43.            // (1) no GetObject in new JScript, and (2) no cross-language scripting.
  44.            function raw_commandline_from_wmi_automation()
  45.            {
  46.                var computer_id = '.' ;
  47.                var wmi = some_get_object_function( 'winmgmts:\\\\' + computer_id + '\\root\\CIMV2' );
  48.                var wbemFlagForwardOnly = 32;
  49.                var wbemFlagReturnImmediately = 16;
  50.                var items = wmi.ExecQuery(
  51.                    'SELECT * FROM Win32_Process WHERE Name = \'mshta.exe\'',
  52.                    'WQL',
  53.                    wbemFlagForwardOnly + wbemFlagReturnImmediately
  54.                    );
  55.                var s = '';
  56.                var this_process;
  57.                var this_usertime;
  58.                for( var it = new Enumerator( items ); !it.atEnd(); it.moveNext() )
  59.                {
  60.                    var process = it.item();
  61.                    var usertime = parseInt( process.UserModeTime );
  62.                    if( this_usertime == undefined || usertime < this_usertime )
  63.                    {
  64.                        this_process = process;
  65.                        this_usertime = usertime;
  66.                    }
  67.                }
  68.                return (this_process == undefined? '' : this_process.CommandLine);
  69.            }
  70.            function is_running( wsh_execution )
  71.            {
  72.                return (wsh_execution.Status == 0);
  73.            }
  74.            function wsh_sleep( wsh_shell, millisecs )
  75.            {
  76.                var nowindow = 0;
  77.                var wait = true;
  78.                wsh_shell.Run( 'ping 1.1.1.1 -n 1 -w ' + millisecs, nowindow, wait );
  79.            }
  80.            function Process_record()
  81.            {
  82.                this.add_item = function( spec )
  83.                {
  84.                    i_delimiter = spec.indexOf( '=' );
  85.                    if( i_delimiter != -1 )
  86.                    {
  87.                        var property_name = spec.substring( 0, i_delimiter );
  88.                        var value = spec.substr( i_delimiter + 1 );
  89.                        this[property_name] = value;
  90.                    }
  91.                }
  92.            }
  93.            function mshta_process_records()
  94.            {
  95.                var wsh_shell = new ActiveXObject( 'WScript.Shell' );
  96.                var execution = wsh_shell.Exec( 'wmic process list full' );
  97.                var records = new Array();
  98.                var current_record = null;
  99.                var n_blank = 0;
  100.                var n_added = 0;
  101.                
  102.                function update_from( line )
  103.                {
  104.                    var i_end = line.length - 1;
  105.                    while( i_end >= 0 && line.substr( i_end, 1 ) < ' ' )
  106.                    {
  107.                        --i_end;
  108.                     }
  109.                     if( 0 <= i_end && i_end < line.length - 1 )
  110.                    {
  111.                        line = line.substr( 0, i_end + 1 );
  112.                    }
  113.  
  114.                    if( is_whitespace( line ) )
  115.                    {
  116.                        ++n_blank;
  117.                        if( current_record != null )
  118.                        {
  119.                            records.push( current_record );
  120.                            current_record = null;
  121.                            ++n_added;
  122.                        }
  123.                    }
  124.                    else //  line.length > 0
  125.                     {
  126.                         if( current_record == null )
  127.                         {
  128.                             current_record = new Process_record();
  129.                         }
  130.                         current_record.add_item( line );
  131.                     }
  132.                 }
  133.                
  134.                 var count1 = 0;
  135.                 while( is_running( execution ) )
  136.                 {
  137.                     //wsh_sleep( wsh_shell, 100 );
  138.                     var line = execution.StdOut.ReadLine();
  139.                     ++count1;
  140.                     update_from( line );
  141.                 }
  142.                 var count2 = 0;
  143.                 while( !execution.StdOut.AtEndOfStream )
  144.                 {
  145.                     var line = execution.StdOut.ReadLine() + '\n';
  146.                     ++count2;
  147.                     update_from( line );
  148.                 }
  149.                 update_from( '' );      // Complete a possible last record.
  150.  
  151.                 return records;
  152.  
  153.                 return ''
  154.                     + count1 + ' + ' + count2 + ' lines processed, '
  155.                     + n_blank + ' blanks, '
  156.                     + n_added + ' records added, '
  157.                     + records.length + ' records.';
  158.             }
  159.  
  160.             // Assumption: a call near the start of this process.
  161.             function record_for_this_process( records )
  162.             {
  163.                 var this_process    = null;
  164.                 var this_usertime   = null;
  165.                 var n               = records.length;
  166.                 for( var i = 0; i != n; ++i )
  167.                 {
  168.                     var process = records[i];
  169.                     if( process.Name == 'mshta.exe' )
  170.                     {
  171.                         var usertime = parseInt( process.UserModeTime );
  172.                         if( this_usertime == null || usertime < this_usertime )
  173.                        {
  174.                            this_process = process;
  175.                            this_usertime = usertime;
  176.                        }
  177.                    }
  178.                }
  179.                return this_process;
  180.            }
  181.  
  182.            function raw_commandline_from_wmic()
  183.            {
  184.                var records         = mshta_process_records();
  185.                var this_process    = record_for_this_process( records );
  186.  
  187.                return (this_process == null? '*' : this_process.CommandLine);
  188.            }
  189.  
  190.            var raw_commandline = raw_commandline_from_wmic;
  191.  
  192.            function Span( _start, _end )
  193.            {
  194.                this.start = _start;
  195.                this.end = _end;
  196.            };
  197.  
  198.            function commandline_argument_spans( s )
  199.            {
  200.                var spans = new Array();
  201.  
  202.                var n = s.length;
  203.                var unquoted = true;
  204.                var in_argument = false;
  205.                var i_span_start;
  206.                for( var i = 0; i <= n; ++i )
  207.                {
  208.                    if( i == n )
  209.                    {
  210.                        if( in_argument )
  211.                        {
  212.                            spans.push( new Span( i_span_start, i ) );
  213.                        }
  214.                        break;
  215.                    }
  216.  
  217.                    var ch = s.substr( i, 1 );
  218.                    if( ch == '"' )
  219.                    {
  220.                        unquoted = !unquoted;
  221.                    }
  222.  
  223.                    if( in_argument )
  224.                    {
  225.                        if( unquoted && is_whitespace( ch ) )
  226.                        {
  227.                            spans.push( new Span( i_span_start, i ) );
  228.                            in_argument = false;
  229.                        }
  230.                    }
  231.                    else
  232.                    {
  233.                        if( !is_whitespace( ch ) )
  234.                        {
  235.                            in_argument = true;
  236.                            i_span_start = i;
  237.                        }
  238.                    }
  239.                }
  240.                return spans;
  241.            }
  242.  
  243.            function commandline()
  244.            {
  245.                var line = raw_commandline();
  246.                var spans = commandline_argument_spans( line );
  247.                var n = spans.length;
  248.                if( n <= 1 )
  249.                {
  250.                    return '';
  251.                }
  252.                else
  253.                {
  254.                    var s = line.substring( spans[1].start, spans[1].end );
  255.                    if( n > 3 )
  256.                     {
  257.                         s += line.substring( spans[2].end );
  258.                     }
  259.                     return s;
  260.                 }
  261.             }
  262.  
  263.             function on_document_loaded()
  264.             {
  265.                 document.getElementById( 'command-line' ).innerText = commandline();
  266.             }
  267.         </script>
  268.     </head>
  269.    
  270.     <body onload="on_document_loaded();" style="background-color: lightGray;" >
  271.         <div style="position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: 1em; border: 1px solid blue; border-radius: 8px; background-color: white; padding: 2em;"/>
  272.         <p>
  273.             <span id="command-line"></span></p>
  274.         </div>
  275.     </body>
  276. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement