Using JavaScript to implement the code _javascript techniques of Windows Task Manager

Source: Internet
Author: User
Tags cpu usage
A lot of people mention JS may think of web pages, in fact, JS can also have the initiative, do some other language can do things, and even can turn to dominate the browser, not by the browser dominate.
This article will introduce a truly available Windows Task Manager that is built on the WSH platform and implemented through JS.
One: Code and comments
Copy Code code as follows:

/*
JS Task Manager
By:x!ao_f
Mail:xiao_f.mail@163.com
qq:120000512
*/
Creates an object Shell object that provides basic functionality such as pop-up prompt boxes, running processes, and manipulating the registry.
var shell = WScript.CreateObject ("Wscript.Shell");
Create WMI objects that allow for system administration through WMI, which includes the Process Management Section
var WMI = WScript.CreateObject ("WbemScripting.SWbemLocator"). ConnectServer (".", "root\\cimv2");
Create an IE window for the output of the data
var browser = WScript.CreateObject ("internetexplorer.application");
Microsoft's Common dialog box, which provides open files, font selection, color selection features
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 ());
}
}

Initializing a window
~function () {
Browser.navigate ("About:blank");
Browser.visible=false
Browser.document.write (' \
<style>\
*{font:14px arial;margin:0;padding:3px;} \
</style>\
<body></body>\
</HTML> ');
Wait for browser load to complete
while (browser. Busy) wscript.sleep (100);
Set Browser appearance
Browser.document.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.document.parentWindow;
Create a button and bind an event
var button = browser.document.createElement ("button");
button.innerhtml = "new process ...";
Button.onmousedown = open;
Browser.document.body.appendChild (button);

var button = browser.document.createElement ("button");
button.innerhtml = "Refresh List";
Button.onmousedown = Refresh;
Browser.document.body.appendChild (button);

var divlist = browser.document.createElement ("div");
Divlist.id = "Divlist";
Browser.document.body.appendChild (divlist);

The callback function that ends the process
browser.document.__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 list
function Update (msg) {
Browser.document.body.all.divList.innerHTML = msg;
}

New process
function open () {
Because the first pop-up dialog box will be blocked by the browser window, hide the browser can solve this problem
if (!commondialog.filter) {
Browser.visible=false
Browser.visible=true
}
This sets the filter rules for opening dialog boxes
Commondialog.filter = "All types (*.*)";
Commondialog.dialogtitle = "Process Manager-Select File";
Commondialog.maxfilesize = 260;
Commondialog.cancelerror = false;
Open a dialog box in a deferred manner to ensure that it is displayed on the front end
Window.settimeout (function () {
Commondialog.showopen ();
var path = Commondialog.filename;
Path is not empty and can be considered a pressed OK button, which is executed with the shell
if (path) {
Shell.run (' "' + Path + '");
Commondialog.filename = ';
Listprocess ();
}
},10);
}

Refresh
function Refresh () {
Listprocess ();
}

Main functions that process a list of processes
function listprocess () {
This is where all of the process basics are queried through WMI, and the use of WMI can be queried for information, or WMIC under cmd-? View help information, not too much here.
var process = WMI. ExecQuery ("SELECT * from Win32_Process");
var p1 = {};
var p2 = {};
The following calculates the CPU occupancy rate, referenced by: 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 Tables and update
var table = [];
var memsum = 0;
var cpusum = 0;
Table.push (' <tr><td> ' +[' serial number ', ' PID ', ' name ', ' Operation ', ' CPU usage ', ' memory footprint ', ' 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.executablepath| | ' -')+' ');
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 footprint: ' + ((~ ~ (memsum*100))/100) + ' MB ' + ', CPU occupancy rate: ' + ((~ ~ (cpusum*100))/100) + '%</td></tr> ');
Table= ' <table width=100% border=1> ' +table.join (') + ' </table> '
Update (table);
}

There is a loop here, because the WScript process is separate from the browser process, and if the program executes without looping, the WScript is over.
try{
while (!browser. Closed) {Try{refresh ();} catch (e) {}; Wscript.Sleep (1000)};
}catch (e) {}

Operation Effect:

Two. SOURCE download
Process.rar
Run method: The premise is the Windows operating system, if the. js is open without special settings, double-click can be run, if associated with the IDE, you need to choose to open the way "Microsoft Windows Based Script Host";
Related Article

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.