Use Javascript to implement the Windows Task Manager Code

Source: Internet
Author: User

Many people may think of web pages when talking about JS. In fact, JS can also take the initiative to do things that can be done in other languages, or even dominate the browser in turn, rather than being dominated by the browser.
This article will introduce a Windows Task Manager that is built on the WSH platform and is truly available through JS.
I. Code and comments
Copy codeThe Code is as follows:
/*
JS Task Manager
By: X! Ao_f
Mail: Xiao_f.mail@163.com
QQ: 120000512
*/
// Create an object Shell object that provides basic functions such as a pop-up prompt box, running processes, and operating the registry.
Var shell = WScript. CreateObject ("WScript. Shell ");
// Create a WMI Object and implement system management through WMI, which includes the Process Management Section.
Var wmi = WScript. CreateObject ("WbemScripting. SWbemLocator"). ConnectServer (".", "root \ cimv2 ");
// Create an IE window for data output
Var browser = WScript. CreateObject ("InternetExplorer. Application ");
// Microsoft's public dialog box component provides the ability to open files, select fonts, and select colors
Var commonDialog = WScript. CreateObject ("MSComDlg. CommonDialog ");
Var window;

Function foreach (object, fn ){
Var I = 0;
For (var e = new Enumerator (object );! E. atEnd (); e. moveNext ()){
Fn (I ++, e. item ());
}
}

// Initialization window
~ Function (){
Browser. navigate ("about: blank ");
Browser. visible = false
Browser.doc ument. write ('\
<HTML> \
<Head> \
<Style> \
* {Font: 14px arial; margin: 0; padding: 3px ;}\
</Style> \
</Head> \
<BODY> </BODY> \
</HTML> ');
// Wait until the browser is loaded
While (browser. Busy) WScript. Sleep (100 );
// Set the browser appearance
Browser.doc ument. title = "Process Manager"
Browser. toolBar = false;
Browser. statusBar = false;
Browser. fullScreen = true;
Var w = browser. width;
Var h = browser. height;
Browser. fullScreen = false;
Browser. width = w;
Browser. height = h;
Browser. left = 0;
Browser. top = 0;
Window = browser.doc ument. parentWindow;
// Create button and bind the event
Var button = browser.doc ument. createElement ("button ");
Button. innerHTML = "new process ..";
Button. onmousedown = open;
Browser.doc ument. body. appendChild (button );

Var button = browser.doc ument. createElement ("button ");
Button. innerHTML = "Refresh list ";
Button. onmousedown = refresh;
Browser.doc ument. body. appendChild (button );

Var divList = browser.doc ument. createElement ("div ");
DivList. id = "divList ";
Browser.doc ument. body. appendChild (divList );

// Callback function for the process to end
Browser.doc ument. _ kill _ = function (pid ){
Var process = wmi. ExecQuery ("Select * From Win32_Process Where ProcessID = '" + pid + "'")
Foreach (process, function (I, o ){
O. terminate ();
});
};
Browser. visible = true
}()

// Update the list
Function update (msg ){
Browser.doc ument. body. all. divList. innerHTML = msg;
}

// Create a process
Function open (){
// Because the first pop-up dialog box will be blocked by the browser window, hiding the browser can solve this problem.
If (! CommonDialog. Filter ){
Browser. visible = false
Browser. visible = true
}
// Set the filter rules in the dialog box
CommonDialog. Filter = "all types (*.*)";
CommonDialog. DialogTitle = "Process Manager-Select File ";
CommonDialog. MaxFileSize = 260;
CommonDialog. CancelError = false;
// Open the dialog box by means of latency to ensure that the dialog box is displayed at the frontend.
Window. setTimeout (function (){
CommonDialog. ShowOpen ();
Var path = commonDialog. Filename;
// If the Path is not empty, it can be regarded as a confirmation button and executed by shell.
If (path ){
Shell. run ('"' + path + '"');
CommonDialog. Filename = '';
ListProcess ();
}
}, 10 );
}

// Refresh
Function refresh (){
ListProcess ();
}

// Main functions for processing the Process List
Function listProcess (){
// WMI is used to query basic information about all processes. You can use WMI to query related information or run wmic-? In cmd -? View help information.
Var process = wmi. ExecQuery ("Select * from Win32_Process ");
Var p1 = {};
Var p2 = {};
// The CPU usage is calculated below, reference: http://www.techish.net/2009/03/get-process-cpu-usage-using-wmi/
Var data1 = wmi. ExecQuery ("select * from Win32_PerfRawData_PerfProc_Process ");
Foreach (data1, function (I, object ){
P1 [object. IDProcess] = object;
});
WScript. sleep (1000)
Var data2 = wmi. ExecQuery ("select * from Win32_PerfRawData_PerfProc_Process ");
Foreach (data2, function (I, object ){
P2 [object. IDProcess] =
(P1 [object. IDProcess]. PercentProcessorTime-object. PercentProcessorTime )/
(P1 [object. IDProcess]. TimeStamp_Sys100NS-object. TimeStamp_Sys100NS) * 100;
});
// Generate and update the table
Var table = [];
Var memSum = 0;
Var cpuSum = 0;
Table. push ('<tr> <td>' + ['sequence number ', 'pid', 'name', 'operation', 'cpu usage ', 'memory usage ', 'path']. join ('</td> <td>') + '</td> </tr> ');
Foreach (process, function (I, object ){
Var tr = [];
Var td = [];
Td. push (I );
Td. push (object. processid );
Td. push (object. name );
Td. push ('<span style = "color: red; cursor: pointer" onclick = "document. _ kill _ ('+ object. processid + '); this. parentNode. parentNode. removeNode (true); "> end </span> ');
Td. push ((~~ (P2 [object. ProcessID] * 100)/100 );
Td. push ((~~ (Object. WorkingSetSize/1024/1024) * 100)/100 + 'mb ');
Td.push(object.exe cutablepath | '-') + '');
MemSum + = (object. WorkingSetSize/1024/1024 );
If (object. processid! = 0 ){
CpuSum + = p2 [object. ProcessID];
}
Tr. push ('<tr> <td>' + td. join ('</td> <td>') + '</td> </tr> ');
Table. push (tr );
});
Table. push ('<tr> <td colspan = 8> memory usage:' + ((~~ (MemSum * 100)/100) + 'mb' + ', CPU usage:' + ((~~ (CpuSum * 100)/100) + '% </td> </tr> ');
Table = '<table width = 100% border = 1>' + table. join ('') + '</table>'
Update (table );
}

// There must be a loop here, because the WScript process is separate from the browser process. If no loop is added, the WScript will end when the program is executed here.
Try {
While (! Browser. Closed) {try {refresh () ;}catch (e) {}; WScript. Sleep (1000 )};
} Catch (e ){}

Running effect:

Ii. Source Code download
Process.rar
Running method: the premise is that the windows operating system, if. the js open mode has no special settings. You can run it by double-clicking it. If you have joined the IDE, You need to select the open mode as "Microsoft Windows Based Script Host ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.