Home >Web Front-end >JS Tutorial >Code to implement Windows Task Manager using Javascript_javascript skills

Code to implement Windows Task Manager using Javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:54:491478browse

When many people mention JS, they may think of web pages. In fact, JS can also take the initiative and do things that other languages ​​can do. It can even dominate the browser instead of being dominated by the browser.
This article will introduce a truly usable Windows task manager built on the WSH platform and implemented through JS.
1: Code and comments

Copy code The code is as follows:

/*
JS Task Manager
By:X!ao_f
Mail:Xiao_f.mail@163.com
QQ:120000512
*/
/ /Create an object Shell object, which provides basic functions such as pop-up prompt boxes, running processes, and operating registry.
var shell = WScript.CreateObject("WScript.Shell");
//Create a WMI object. System management can be achieved through WMI, which includes the process management part.
var wmi = WScript.CreateObject( "WbemScripting.SWbemLocator").ConnectServer(".", "root\cimv2");
//Create an IE window for data output
var browser = WScript.CreateObject("InternetExplorer.Application" ; >
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.document.write('





');
//Wait for the browser to load
while(browser.Busy) WScript.Sleep(100);
//Set the 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 events
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);

//Callback function to end 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, hiding the browser can solve this problem Question
if(!commonDialog.Filter){
browser.visible=false
browser.visible=true
}
//Here set the filtering rules for opening the dialog box
commonDialog. Filter = "All types (*.*)";
commonDialog.DialogTitle = "Process Manager - Select File";
commonDialog.MaxFileSize = 260;
commonDialog.CancelError = false;
/ / Opening the dialog box in a delayed manner can ensure that it is displayed at the front end
window.setTimeout(function(){
commonDialog.ShowOpen();
var path = commonDialog.Filename;
//Path If it is not empty, it can be regarded as pressing the OK button, and use the shell to execute the path
if(path){
shell.run('"' path ''');
commonDialog.Filename = '';
listProcess();
}
},10);
}

//Refresh
function refresh(){
listProcess();
}

//The main function for processing the process list
function listProcess(){
//Here we first query the basic information of all processes through WMI. You can query relevant information about how to use WMI. Or run wmic -? under cmd to view the help information. I won’t introduce it in detail here.
var process = wmi.ExecQuery("Select * from Win32_Process");
var p1 = {};
var p2 = {};
//Calculate the CPU usage below, quote: 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 table and update
var table = [];
var memSum = 0;
var cpuSum = 0;
table.push('' ['serial number','PID','name','operation','CPU usage','memory usage',' Path'].join('') '');
foreach(process, function(i, object){
var tr = [];
var td = [];
td.push(i);
td.push(object.processid);
td.push(object.name);
td.push('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('' td.join( '') '');
table.push(tr);
});
table.push(' Memory usage:' ((~~(memSum*100))/100) 'MB' ',CPU usage:' ((~~(cpuSum*100))/100 ) '%');
table='' table.join('') '
'
update(table);
}

//There needs to be a loop here because the WScript process is separate from the browser process. If there is no loop, WScript will end when the program is executed here.
try{
while(!browser.Closed) { try{refresh();}catch(e){}; WScript.Sleep(1000) };
}catch(e){}

Operating effect:

2. Source code download
Process.rar
Running method: The premise is Windows operating system. If the opening method of .js has not been specially set, double-click it. It can be run. If it is associated with an IDE, you need to select the opening method as "Microsoft Windows Based Script Host";
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn