node-webkit浏览器插件注册升级方式,调用srver32,reg等系统层方法来实现。本人对这些方法封装实现了插件检查是否注册,注册表插件版本检查,升级功能,写成一个公共模块分享出来。
var async = require('async'); var path = require('path'); var exec = require('child_process').exec; var gui = global.window.nwDispatcher.requireNwGui(); /*插件对像格式,要传入这个格式的对像去执行各项操作*/ var plugin={ version:"1", name:"chrScpCaptureKTP", description:"screenCapture",//不能有空格 filename:"chrScpCapKTP.dll", regLocal:"HKEY_LOCAL_MACHINE\\SOFTWARE\\MozillaPlugins\\chrScpCap", dll:[path.join(__dirname,"../../lib/ScreenCapture/ScreenCaptureKTP.dll")], path:path.join(__dirname,"../../lib/ScreenCapture/chrScpCapKTP.dll") } var Register = function(){ var THIS=this; /* 注册系统组件 *输入DLL文件名时,如果待处理的是非系统文件,必须在文件名前添加文件绝对路径,必须注意的是文件路径不包含中文,否则很可能导致处理失败。 *regsvr32 [/u] [/s] [/n] [/i[:cmdline]] DLL文件名 */ this.regsvr32 = function(method,dllPath,callback){ if(method!=="add"&&method!=="delete"){ return callback("regsvr32不支持该方法") } try{ var parm = (method == "delete")?" /u ":" "; var regStr ="regsvr32 /s"+ parm+" "+dllPath; console.log("REG32",regStr) exec(regStr,{encoding: 'utf8'},callback); }catch(err){ console.err("注册系统组件失败",err) callback("注册系统组件失败"+err); } }; /* 注册表注册 * method 类别: "add"|| "delete"||"query" * keyName 名称,如: "HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" * entryName 项名称 * value 写入值, "String" * valueType 值类型, "REG_SZ"等注册表值类型 * force bool, 强行覆盖已存在的键值 */ this.reg = function(method,keyPath,KeyName,value,valueType,force,callback){ if(method!=="add"&&method!=="delete"&&method!="query"){ return callback("注册表不支持该方法") } try{ var regStr ="reg "+ method+" "+keyPath; if(KeyName){ regStr += " /v "+KeyName; } if(value){ regStr += " /d "+value; } if(valueType){ regStr += " /t "+valueType; } if(force){ regStr += " /f"; } console.log("REG",regStr) exec(regStr,{encoding: 'utf8'},callback); }catch(err){ console.error("注册表操作失败",err); callback("注册表操作失败"+err); } }; // 通用注册插件到注册表 this.execreg =function(plugin,callback){ try{ async.auto({ regMenu: function(cb,results){ THIS.reg("add",plugin.regLocal,null,null,null,true,cb) }, regDescription: ["regMenu",function (cb, results) { THIS.reg("add",plugin.regLocal,"Description",plugin.description,"REG_SZ",true,cb) }], regPath: ["regMenu",function (cb, results) { THIS.reg("add",plugin.regLocal,"Path",plugin.path,"REG_SZ",true,cb) }], regProductName: ["regMenu",function (cb, results) { THIS.reg("add",plugin.regLocal,"ProductName",plugin.name,"REG_SZ",true,cb) }], regVersion: ["regMenu",function (cb, results) { THIS.reg("add",plugin.regLocal,"Version",plugin.version,"REG_SZ",true,cb) }] }, function(err, results) { console.log("execreg完成",plugin.name,err,results) callback&&callback(err,results) }); }catch(err){ console.error("REG"+plugin.name,err) callback&&callback(err) } }; // 安装插件 this.installPlugin = function(plugin,callback){ async.auto({ reg32:function(cb,results){ async.each(plugin.dll,function(dll,each_cb){ THIS.regsvr32("add",dll,each_cb); },cb) }, regedit:["reg32",function(cb,results){ THIS.execreg(plugin,cb) }] },function(err,results){ if(err){ console.error("安装插件失败",err) }else{ console.log("安装插件成功",results) } callback(err,results) }) }; // 移除插件 this.removeplugin = function(plugin,callback){ async.auto({ reg32:function(cb,results){ async.each(plugin.dll,function(dll,each_cb){ THIS.regsvr32("delete",dll,each_cb); },cb) }, regedit:["reg32",function(cb,results){ THIS.reg("delete",plugin.regLocal,"","","",true,cb) }] },function(err,results){ if(err){ console.error("移除插件失败",err) }else{ console.log("移除插件成功",result) } callback(err,results) }) }; // 检查插件是否存在 this.checkPlugin = function(plugin){ for (var i = 0, l = global.window.navigator.plugins.length; i < l; i++) { if (global.window.navigator.plugins[i].filename == plugin.filename) { return true; } } return false; }; // 获取插件版本 this.getPluginVersion = function(plugin,callback){ var regLocal=plugin.regLocal; reg("query",regLocal,"Version",null,null,null,function(err,stdout,stderr){ var version=0; if(stdout.match(/[\d.]+/ig)){ version=stdout.match(/[\d.]+/ig)[0]; } callback&&callback(err,version,stderr) }) }; // 升级插件 this.updatePlugin = function(plugin,callback){ console.log("updatePlugin","getPLugnin",this.checkPlugin(plugin),plugin.name) if(!this.checkPlugin(plugin)){ this.installPlugin(plugin,callback) }else{ this.getPluginVersion(plugin,function(err,version){ if(err){ callback(err) }else{ console.log("updatePlugin","version",plugin.version) if(version<plugin.version){ THIS.installPlugin(plugin,callback) }else{ callback(null) } } }) } }; } var register = exports = module.exports = new Register(); module.exports.Register = Register;