Getting Compile error in Microsoft JScript (expected) - javascript

I tested a code with my friend and the code worked and now I wanted to test this again and got a Compile error in Microsoft JScript
Everything worked before
var iTest = 0;
if (iTest) {
let sText = "This is a template string which can contain variables like ${iTest}";
console.log(sText);
} else if (!iTest) {
let fnc = (iRadius, iPi = 3.14) => {
console.log("The area of the circle with the radius ${iRadius}cm is ${Math.round(iPi * iRadius * iRadius)}cm²");
}
fnc(3.14);
} else {
/* This is never going to be called since else if is already handling the only option besides the if */
}
var fnc = (iAlpha, iBravo, cb) => {
cb(iAlpha + iBravo);
}
fnc(84,255,(iResult) => {
console.log(iResult);
});

Template literals and arrow functions are not available in JScript. The latest version of JScript is based on ES5, and those two features are only available in ES2015. And console is not available in JScript if you are using the Windows Script Host; instead, use WScript.Echo().
var iTest = 0;
if (iTest) {
var sText = "This is a template string which can contain variables like " + iTest;
WScript.Echo(sText);
} else {
var fnc = function(iRadius, iPi) {
iPi = 3.14;
WScript.Echo("The area of the circle with the radius " + iRadius + "cm is " + Math.round(iPi * iRadius * iRadius) + "cm²");
}
fnc(3.14);
}
var fnc = function(iAlpha, iBravo, cb) {
cb(iAlpha + iBravo);
}
fnc(84, 255, function(iResult) {
WScript.Echo(iResult);
});
It would also be nice if you could provide more information about the particular error you are getting. This should fix it, but it would make it easier for us to help if we know exactly what the problem is.

Related

Complex Javascript for performing a basic job

I came across a website that was using a very strange piece of code with a lot of variables and recursive functions just to forward the user to the main website. The code was obfuscated. After deobfuscation, it is as follows. Note that the go() function is executed upon click of a button named 'Enter Site':
(function (paetynn, jaquez) {
var lyons = quetzali, payzli = paetynn();
while (!![]) {
try {
var kyona = parseInt(lyons(295)) / 1 + -parseInt(lyons(300)) / 2 + parseInt(lyons(299)) / 3 + -parseInt(lyons(301)) / 4 * (parseInt(lyons(304)) / 5) + -parseInt(lyons(294)) / 6 * (parseInt(lyons(298)) / 7) + parseInt(lyons(297)) / 8 * (-parseInt(lyons(303)) / 9) + parseInt(lyons(305)) / 10;
if (kyona === jaquez) break; else payzli.push(payzli.shift());
} catch (zulekha) {
payzli.push(payzli.shift());
}
}
}(niree, 850584));
function quetzali(winston, ikeshia) {
var hongan = niree();
return quetzali = function (khole, kysin) {
khole = khole - 294;
var zebulan = hongan[khole];
return zebulan;
}, quetzali(winston, ikeshia);
}
function go() {
var amey = quetzali, gracelee = amey(296);
document[amey(302)] = gracelee, window.location = "/";
}
function niree() {
var saana = ["604264sauqJi", "10214183nDqKAH", "2078325AmAiPo", "1206860TDYYcJ", "4VhIfuJ", "cookie", "45pazGrL", "380390DQkmbn", "12476230senONL", "6bRFHSG", "1426528LpTiXE", "session_keys=mycheck;"];
niree = function () {
return saana;
};
return niree();
}
I don't fully understand what the code does. Could some JS expert clarify on the piece?
Seems like the code is manipulating window.location.
Also it was used inside a script tag inside the head tag of the page.
This code sets cookie session_keys=mycheck; and then redirects to /. The rest of code make permutation in the array of strings saana so analizyng code would be more difficult.

Access to filesystem from WinDBG's JavaScript script

I'm currently playing with WinDBG script written in JavaScript as it's described by Microsoft.
How can I access the filesystem from within the JavaScript code? I'm interested in both reading and writing to files located somewhere on disk. For JavaScript executing on the browsers these features are disabled because of security reasons but for example NodeJS has its own libraries to support filesystem operations.
This works:
"use strict";
function invokeScript() {
var debugControl = host.namespace.Debugger.Utility.Control;
var output = debugControl.ExecuteCommand("vertarget");
writeOutputToFile(output);
}
function writeOutputToFile(output) {
var logFilePath = "c:\\debugging\\output\\output.log";
var logFile;
if (host.namespace.Debugger.Utility.FileSystem.FileExists(logFilePath)) {
logFile = host.namespace.Debugger.Utility.FileSystem.CreateFile(logFilePath, "OpenExisting");
}
else {
logFile = host.namespace.Debugger.Utility.FileSystem.CreateFile(logFilePath);
}
var textWriter = host.namespace.Debugger.Utility.FileSystem.CreateTextWriter(logFile, "Utf16");
try {
for (var line of output) {
textWriter.WriteLine(line);
}
}
finally {
logFile.Close();
}
}
I tried File, Blob and ActiveXObject as suggested throughout the Internet, but none of them works in WinDbg.
You could try a combination of .dvalloc + .writemem + .dvfree. Below is a starting point, but far from complete:
function saveTextAsFile()
{
var dbgOut = host.diagnostics.debugLog;
var exec = host.namespace.Debugger.Utility.Control.ExecuteCommand;
var output = exec(".dvalloc 0x10000");
for (var line of output)
{
dbgOut("Output: "+line+"\n");
var index = line.indexOf("starting at ");
var address = line.substring(index+("starting at ".length));
dbgOut("Allocated memory at "+address+"\n");
exec(".writemem f:\\debug\\logs\\fromscript.txt "+address+" L10000")
var output = exec(".dvfree " + address + " 0x10000");
break;
}
}

Local variable becomes undefined in callback function in javascript

Im a java programmer trying to write javascript cant seem to grasp how scope changes when callbacks are called.
bot.sendmessage below failes to run. Error log says "undefined.bot.sendMessage(formId, resp)"
"use strict";
function Handlers() {
this.bot = undefined;
this.registerHandler = (bot)=>{
this.bot = bot;
bot.on('message', this._messageCallback);
bot.on('inline_query', this._inlineCallback)
}
}
Handlers.prototype = {
_messageCallback: (msg) => {
console.log("New Outline Request from: chat=" + msg.chat.id + ", uid=" + msg.from.id);
var fromId = msg.from.id;
var resp = "Hello there";
this.bot.sendMessage(fromId, resp);
},
_inlineCallback: (msg) => {
console.log("New Inline Request from: uid=" + msg.from.id);
/*
TODO::
check if user is in data base if not log new entry;
*/
}
};
module.exports = Handlers;
First I need to say Teemu is correct. This is an issue with Arrow Functions which I myself am unfamiliar with. If you would like to see an alternative way of acomplishing the same thing without using Arrow Functions take a look at the snippet below.
This code I would change later to be an Immediately-Invoked Function Expression (IIFE) but that is my personal programming habits. I don't know your exact use case here.
"use strict";
/**
* I'm assuming you have this elsewhere?
* If not I have added it to show how to do it
*/
function bot(){
/* ... */
}
bot.prototype = {
sendMessage: function(formId,resp){
console.log("Form: "+formId+" | Message: "+resp);
}
};
/**
* End of my addition
*/
function Handlers() {
this.bot = new bot();
/* ... */
}
Handlers.prototype = {
_messageCallback: function(msg){
/* ... */
var fromId = "fakeid123";
var resp = msg;
this.bot.sendMessage(fromId, resp);
},
_inlineCallback: function(msg){
/* ... */
}
};
var myModule= new Handlers();
myModule._messageCallback("Here is my test msg.");

How to define "interface" in js

Good day.
My question is quite dumb, I guess, but I'm not familiar enough with the termins, to ask it properly (and to get an answer from Google).
So - please help...
Shortly - I'm trying to create some major class, which will be insanitated by instances, which will describe some methods, and some fields.
Major logick will be implemented in parent class.
So, lets say I have a parent
function CRUD_Grid_model(){
//Settings part
this.GridElement = "" ;
this.editModeFlagElement = "" ;
this.newRowElement = "";
//Save logicks all lies here.
this.commit = function (){
alert("PLEASE REDEFINE COMMIT FUNCTION IN YOUR CODE");
}
;
//Settings part
//Some more code.
}
And a way I'll use it
//Das modell
var JobberCRUD = new CRUD_Grid_model();
JobberCRUD.GridElement = $('#jobbers_dg');
JobberCRUD.editModeFlagElement = $('#jobbers_tb_edit');
JobberCRUD.newRowElement = {jobb_name:'Enter new unique name',jobb_status:'Y'};
JobberCRUD.commit = function (){
if (this.endEditing()){
var addrows = this.GridElement.datagrid('getChanges','inserted');
var remrows = this.GridElement.datagrid('getChanges','deleted');
var updrows = this.GridElement.datagrid('getChanges','updated');
console.log(addrows);
console.log(remrows);
console.log(updrows);
//Send changes?
alert("Got total of " +addrows.length + remrows.length + updrows.length + " rows changed.");
//Commit changes at local level
this.GridElement.datagrid('acceptChanges');
}
};
And, what I'd like to do, is smoething like this
I want a parent.commit function to allow me to do this in child
JobberCRUD.commit = function (apdrows,updrows,remrows){
//Send changes?
alert("Got total of " +addrows.length + remrows.length + updrows.length + " rows changed.");
};
So, I have no ideas what shoudl I do to achieve that. Please advice me with some tags, what it is at least :)
Thanks in advance.
There is not exactly what you need in JavaScript, but I would tend to use this pattern :
function CRUD_Grid_model() {
...
this.onCommit = null;
this.commit = function (){
if (this.endEditing()){
var addrows = this.GridElement.datagrid('getChanges','inserted');
var remrows = this.GridElement.datagrid('getChanges','deleted');
var updrows = this.GridElement.datagrid('getChanges','updated');
if(this.onCommit != null) this.onCommit(addrows,updrows,remrows);
this.GridElement.datagrid('acceptChanges');
}
}
...
}
var JobberCRUD = new CRUD_Grid_model();
JobberCRUD.onCommit = function(apdrows,updrows,remrows) {
alert("Got total of " +addrows.length + remrows.length + updrows.length + " rows changed.");
};
JobberCRUD.GridElement = ...
Javascript doesn't have a built in notion of interfaces. A javascript object effectively "implements an interface" by having all the needed methods defined, but there's no language "interface" construct
You could use "extends" like so:
Object.prototype.extends = function(clazz) {
var o = new clazz();
for (var f in o) {
if(f === "extends") {
continue;
}
this[f] = o[f];
}
this.super = o;
}
Now could type something like this:
var JobberCRUD = function() {
this.extends(CRUD_Grid_model);
var privateFunction = function() {
//...
}
// You probably don't want to do that,
// but you could override
this.commit = function() {
//...
}
// ...
}
Hope, that helps.
Edit: forgot, that you may call super.commit() then.
You can not have something similar like C# or Java interface or even class with Javascript.
Javascript is just a dynamic scripting language.

Firefox not catching "undefined" error in javascript

I have some javascript code which calls a servlet with parameters based on the selected option from a SELECT list. It is possible that there will be no options in the selectCampaigns(account) so there is a try and catch.
The code in the catch should be called if,
var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
fails. This works in both chrome and IE but in firefox it gives a TypeErrror,
TypeError: selectCampaigns.options[selectCampaigns.selectedIndex] is undefined
which does not trigger the catch. May I have some suggestions on how to handle this? (in addition firefox does not show any of the alerts that I have inserted for debugging).
function selectcampaign(account) {
alert('alert1');
var selectCampaigns = document.getElementById("campaignSelect");
var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart";
try {
var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account + "&campaign=" + selectedCampaign;
} catch(err) {
alert(err);
urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
} finally {
window.location.href = urlstrg;
}
};
You should better test the value instead of dealing with errors.
function selectcampaign(account) {
var selectCampaigns = document.getElementById("campaignSelect");
var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
if(selectCampaigns.options[selectCampaigns.selectedIndex]) {
urlstrg += "&campaign=" + selectCampaigns.options[selectCampaigns.selectedIndex].text;
}
window.location.href = urlstrg;
};

Categories

Resources