From 2b66c4959da3cfe89d1946dba50a2d2c616572d9 Mon Sep 17 00:00:00 2001 From: fofolee Date: Wed, 27 Mar 2019 15:55:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Get-Process=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=20tasklist?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- preload.js | 83 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/preload.js b/preload.js index 33c7063..4658a0f 100644 --- a/preload.js +++ b/preload.js @@ -1,26 +1,59 @@ -const exec = require('child_process').exec; -const iconv = require('iconv-lite'); - -window.tasklist = (callback) => { - // if(process.platform === 'win32'){} - let cmd = 'tasklist /FO csv /NH /V' - exec(cmd, function (err, stdout, stderr) { - let tasklist = []; - let lines = stdout.trim().split('\n'); - for (var line of lines){ - tasklist.push(line.trim().split(',')); - } - callback(tasklist); - }) -} - -window.taskkill = (taskname, callback) => { - let cmd = 'TASKKILL /F /IM "' + taskname + '" /T' - exec(cmd, { encoding: "buffer"}, (err, stdout, stderr) => { - if (err) { - callback(iconv.decode(stderr,'cp936')); - return; - } - callback(iconv.decode(stdout,'cp936')); - }) +const fs = require('fs'); +const path = require("path"); +const iconExtractor = require('icon-extractor'); +const os = require('os') +const PowerShell = require("powershell"); + + +getico = tasks =>{ + iconExtractor.emitter.on('icon', function (data) { + let icondir = path.join(os.tmpdir(), 'ProcessIcon') + fs.exists(icondir, exists => { + if (!exists) { fs.mkdirSync(icondir) } + let iconpath = path.join(icondir, `${data.Context}.png`) + fs.exists(iconpath, exists => { + if (!exists) { + fs.writeFile(iconpath, data.Base64ImageData, "base64", err => { + if (err) { console.log(err); } + }) + } + }) + }) + }); + + for (var task of tasks) { + iconExtractor.getIcon(task.ProcessName, task.Path); + } +} + +tasklist = (callback) => { + let ps = new PowerShell("chcp 65001;Get-Process | Format-List ProcessName,Path,Description"); + ps.on("output", data => { + let tasklist = []; + let tasks = data.trim().split('\r\n\r\n'); + for (var task of tasks) { + dict = {} + let lines = task.split('\r\n') + for (var line of lines) { + if (line) { + let key = line.split(/\s+:\s*/)[0]; + let value = line.split(/\s+:\s*/)[1]; + dict[key] = value; + } + } + var icon = path.join(os.tmpdir(), 'ProcessIcon', `${dict.ProcessName}.png`); + dict.Icon = icon + tasklist.push(dict); + } + tasklist.shift(); + getico(tasklist); + callback(tasklist); + }); +} + +taskkill = (taskname, callback) => { + let ps = new PowerShell(`chcp 65001;Stop-Process -Name ${taskname}`); + ps.on("error-output", data => { + callback(data.split('\n')[0]) + }); } \ No newline at end of file