Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <!-- Copyright (c) 2013 Alf P. Steinbach -->
- <!-- How to retrieve a command line for an IE10-based Windows HTML-Application (HTA). -->
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <!-- A windows web control defaults to quirky IE7 semantics. Request for better: -->
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta http-equiv="MSThemeCompatible" content="yes">
- <title>Command line</title>
- <style>
- * { font: 10pt 'MS Shell Dlg 2'; }
- #command-line {
- background-color: lightBlue;
- }
- </style>
- <script type="text/javascript">
- function is_whitespace( s )
- {
- var n = s.length;
- if( n == 1 )
- {
- return (s == ' ' || s == '\n' || s == '\r' || s == '\t');
- }
- else
- {
- for( var i = 0; i < n; ++i )
- {
- if( !is_whitespace( s.substr( i, 1 ) ) )
- {
- return false;
- }
- }
- return true;
- }
- }
- // This function doesn't work when using IE10 engine or better, because
- // (1) no GetObject in new JScript, and (2) no cross-language scripting.
- function raw_commandline_from_wmi_automation()
- {
- var computer_id = '.' ;
- var wmi = some_get_object_function( 'winmgmts:\\\\' + computer_id + '\\root\\CIMV2' );
- var wbemFlagForwardOnly = 32;
- var wbemFlagReturnImmediately = 16;
- var items = wmi.ExecQuery(
- 'SELECT * FROM Win32_Process WHERE Name = \'mshta.exe\'',
- 'WQL',
- wbemFlagForwardOnly + wbemFlagReturnImmediately
- );
- var s = '';
- var this_process;
- var this_usertime;
- for( var it = new Enumerator( items ); !it.atEnd(); it.moveNext() )
- {
- var process = it.item();
- var usertime = parseInt( process.UserModeTime );
- if( this_usertime == undefined || usertime < this_usertime )
- {
- this_process = process;
- this_usertime = usertime;
- }
- }
- return (this_process == undefined? '' : this_process.CommandLine);
- }
- function is_running( wsh_execution )
- {
- return (wsh_execution.Status == 0);
- }
- function wsh_sleep( wsh_shell, millisecs )
- {
- var nowindow = 0;
- var wait = true;
- wsh_shell.Run( 'ping 1.1.1.1 -n 1 -w ' + millisecs, nowindow, wait );
- }
- function Process_record()
- {
- this.add_item = function( spec )
- {
- i_delimiter = spec.indexOf( '=' );
- if( i_delimiter != -1 )
- {
- var property_name = spec.substring( 0, i_delimiter );
- var value = spec.substr( i_delimiter + 1 );
- this[property_name] = value;
- }
- }
- }
- function mshta_process_records()
- {
- var wsh_shell = new ActiveXObject( 'WScript.Shell' );
- var execution = wsh_shell.Exec( 'wmic process list full' );
- var records = new Array();
- var current_record = null;
- var n_blank = 0;
- var n_added = 0;
- function update_from( line )
- {
- var i_end = line.length - 1;
- while( i_end >= 0 && line.substr( i_end, 1 ) < ' ' )
- {
- --i_end;
- }
- if( 0 <= i_end && i_end < line.length - 1 )
- {
- line = line.substr( 0, i_end + 1 );
- }
- if( is_whitespace( line ) )
- {
- ++n_blank;
- if( current_record != null )
- {
- records.push( current_record );
- current_record = null;
- ++n_added;
- }
- }
- else // line.length > 0
- {
- if( current_record == null )
- {
- current_record = new Process_record();
- }
- current_record.add_item( line );
- }
- }
- var count1 = 0;
- while( is_running( execution ) )
- {
- //wsh_sleep( wsh_shell, 100 );
- var line = execution.StdOut.ReadLine();
- ++count1;
- update_from( line );
- }
- var count2 = 0;
- while( !execution.StdOut.AtEndOfStream )
- {
- var line = execution.StdOut.ReadLine() + '\n';
- ++count2;
- update_from( line );
- }
- update_from( '' ); // Complete a possible last record.
- return records;
- return ''
- + count1 + ' + ' + count2 + ' lines processed, '
- + n_blank + ' blanks, '
- + n_added + ' records added, '
- + records.length + ' records.';
- }
- // Assumption: a call near the start of this process.
- function record_for_this_process( records )
- {
- var this_process = null;
- var this_usertime = null;
- var n = records.length;
- for( var i = 0; i != n; ++i )
- {
- var process = records[i];
- if( process.Name == 'mshta.exe' )
- {
- var usertime = parseInt( process.UserModeTime );
- if( this_usertime == null || usertime < this_usertime )
- {
- this_process = process;
- this_usertime = usertime;
- }
- }
- }
- return this_process;
- }
- function raw_commandline_from_wmic()
- {
- var records = mshta_process_records();
- var this_process = record_for_this_process( records );
- return (this_process == null? '*' : this_process.CommandLine);
- }
- var raw_commandline = raw_commandline_from_wmic;
- function Span( _start, _end )
- {
- this.start = _start;
- this.end = _end;
- };
- function commandline_argument_spans( s )
- {
- var spans = new Array();
- var n = s.length;
- var unquoted = true;
- var in_argument = false;
- var i_span_start;
- for( var i = 0; i <= n; ++i )
- {
- if( i == n )
- {
- if( in_argument )
- {
- spans.push( new Span( i_span_start, i ) );
- }
- break;
- }
- var ch = s.substr( i, 1 );
- if( ch == '"' )
- {
- unquoted = !unquoted;
- }
- if( in_argument )
- {
- if( unquoted && is_whitespace( ch ) )
- {
- spans.push( new Span( i_span_start, i ) );
- in_argument = false;
- }
- }
- else
- {
- if( !is_whitespace( ch ) )
- {
- in_argument = true;
- i_span_start = i;
- }
- }
- }
- return spans;
- }
- function commandline()
- {
- var line = raw_commandline();
- var spans = commandline_argument_spans( line );
- var n = spans.length;
- if( n <= 1 )
- {
- return '';
- }
- else
- {
- var s = line.substring( spans[1].start, spans[1].end );
- if( n > 3 )
- {
- s += line.substring( spans[2].end );
- }
- return s;
- }
- }
- function on_document_loaded()
- {
- document.getElementById( 'command-line' ).innerText = commandline();
- }
- </script>
- </head>
- <body onload="on_document_loaded();" style="background-color: lightGray;" >
- <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;"/>
- <p>
- <span id="command-line"></span></p>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement