Jint+JSfuck - 'Index was outside the bounds of the array' - javascript

I'm trying to run the following code in jint:
Jint.Engine engine = new Jint.Engine();
var result = engine.SetValue("data", data).Execute("(/\\n(.+)/.exec(eval(data.replace(/\\s+/, \"\").slice(0, -2)))[1]);").GetCompletionValue();
Which, when unescaped, is executing the following javascript:
(/\n(.+)/.exec(eval(data.replace(/\s+/, "").slice(0, -2)))[1]);
the data variable corresponds to a JSfuck string, similar to this: https://pastebin.com/vmGAebW5
The problem is that I always get a 'Index was outside the bounds of the array' exception, even though the javascript works fine when run in a browser. Any ideas as to what is causing the issue?

windows.location is not available in Jint as the window object is provided by the browser. If you wanted to be able to run this kind of code then you would need to mock all the browser exposed objects, which is a huge task.
Also you are using SetValue('window.location', "") which I assume is not a valid call. The property name should not contain dots.

Related

Object property in Javascript exists in the larger object but is null when accessing just that property

I'm new to JavaScript (and most coding concepts in general) and this feels like there must be some simple solution that I just haven't found yet. I need information from a property of an object (not totally sure if I'm using those terms correctly). The object is created using ArcGIS API for Javascript.
console.log(view.popup);
console.log(view.popup.id);
console.log(view.popup.title);
By logging to the console (using the lines of code shown above) I can see that the properties exist because the below lines are logged to the console (from Line 1).
id: "17e2bf83e50-widget-1"
title: "k0"
I then log just the id property (Line 2) and it prints just the id property. However, if I try to log view.popup.title (Line 3), it logs 'null' to the console. Anything else I try to print out using console.log prints the same value found within view.popup. I just need to be able to use that value stored within view.popup.title and for some reason it seems like the only one where I can see that it's there but can't access it directly?
Edit: This does certainly seem to be the issue commented on by folks below, thanks for those links! I've been trying to do stringify (as suggested in Is Chrome’s JavaScript console lazy about evaluating objects?) but finding that it only finds some of the properties. I also made an attempt at making it wait to try to find it until the property is no longer null, but it's definitely somehow later in the code where that happens (the code just enters an infinite loop until there's a call error).
View is generated using https://developers.arcgis.com/javascript/latest/api-reference/esri-views-SceneView.html#popup (which is why I don't have a full understanding of what goes into it/when and how it makes that view.popup.title property)
view = new SceneView({
viewingMode: "local",
map: scene,
container,
camera,
spatialReference: {
wkid:3089
}
});
After learning here that it's related to view.popup.title not existing yet at the point of logging it, I moved over to the ArcGIS forum to ask about how view is generated and I got an answer that lets me access it! https://community.esri.com/t5/arcgis-api-for-javascript-questions/accessing-title-of-default-popup-created-for-a/m-p/1131210#M75813
view.popup.watch("title", () => {
console.log(view.popup.title);
});

javascript expression expected

I am still new a Javascript tried earching and tried the development tool in Chrome, too see if I could find the problem.
Working in Intellij IDEA 13, Java, javascript and xhtml.
My problem is that I have a piece of javascript, then in IDEA when moused over, says that
Expression Expected
the javascript code looks the following
<script type="text/javascript>
function nameOfFunction(){
if(#{trendAnalysisLocationReportController.model.showTargetLine}){
this.cfg.series[this.cfg.data.length-1].pointLabels = {
show: false
};
}
}
<\script>
the method in the if sentence is a java method with a boolean return value.
the error is shown when hovering
'#{'
if Had a look at the following questions, before :
Expected Expression
boolean in an if statement
But didnt get me a solution.
what Iam I doing wrong ?
It looks as though the problem is the part that you've got within the #{...} block. Without knowing the context, it's hard to be sure, but is this something in a view/JSP page that's supposed to be replaced with a property at runtime? The if block will expect the part inside the brackets to be a boolean value, so if that's rendered to either 'true' or 'false' it would execute at runtime but would likely show the error you're seeing in your IDE as it's not actually a valid piece of JavaScript. If, on the other hand, you're expecting to be able to call your Java method/property from your JavaScript code, you're going to need to do something that requests that value from the server-side code - AJAX or similar.
Also worth noting that we can't see what this.cfg is supposed to represent. If that's your entire script block, then there's nothing that defines the cfg object within the current scope.
One last thing, you should change the <\script> end element to as it won't be understood properly by some browsers.

IE8 Dot versus Bracket Notation

I'm having a strange issue in IE8 where I'm trying to grab something by simply doing:
window.frames.frames[0].name; // get the name of the inner iFrame object
Nothing fancy, but when script is ran, IE7-8 interpret it like this:
window.frames.frames.0.name;
// which in-turn gives the error
// 'window.frames.frames.0.name' is null or not an object (which is not true)
Why and how is it converting this, and why isn't it even working anymore??
If I type the first one window.frames.frames[0].name; into the console of IE8, it grabs the correct iFrame. But typing in what IE8 interprets (window.frames.frames.0.name;), doesn't work at all... (strangely says, "Expected ';'", which makes zero sense haha.
Anyone ever run into an issue like this?
That dot notation in the error message is just a string the browser uses, poor choice on the browser developers.
The line `window.frames.frames[0].name` does not make sense.
I would expect
window.frames[0].name
or if it is nested frame in a frame
window.frames[0].frames[0].name
window.frames is an array, is it not? Shouldn't you be indexing the first frame?
window.frames[0].frames[0].name;
Does it work if you put parentheses around the the call? like this:
(window.frames.frames[0]).name; // get the name of the inner iFrame object
Also do you really mean do reference window.frames.frames[0] and not just window.frames[0]?
Or do you mean:
window.frames[0].frames[0].name; // get the name of the inner iFrame object

Unable to create a MozillaBrowserBot object

I tried to get the MozillaBrowserBot object in mozilla js. But it is not giving the object. I used the code as below:
function externalApplication(){
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
alert("wm: "+wm);
var contentWindow=wm.getMostRecentWindow('navigator:browser').getBrowser().contentWindow;
alert("contentWindow: "+contentWindow);
//I am not gettting this pageBot object
var pagebot=new MozillaBrowserBot(contentWindow);
alert(pagebot);
}
I want to add the find option to the xpath checker. If the MozillaBrowserBot is related to selenium IDE then is there any possibility to get the pagebot object?
Judging by Google search results, MozillaBrowserBot is something that's defined by Selenium IDE. Also, it is apparently defined in the content page you got, not in the context where your code executes. That means that the proper invocation would be:
var pagebot = new contentWindow.MozillaBrowserBot(contentWindow);
This is based on a bunch of guesses of course since your question doesn't provide any context information whatsoever.

How do I relate a UIWebView javascript exception's sourceId to a source file?

Short question: What does an exception's "sourceID" refer to, and how can I link it to the relevant source string/file?
Longer story:
I am running Javascript code in an iPhone native app through [UIWebView stringByEvaluatingJavaScriptFromString:]. To help development, and later check user-provided code, I use the following function to safely run any code:
// Inside #implementation MyJS
- (NSString *)runJS:(NSString *)js {
// Do some escaping on 'js' to make it look like a string literal.
js = escape(js);
NSString *result =
[webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:#"try { JSON.stringify(eval(\"%#\")); } except (e) { JSON.stringify(e); }", js]
];
return result;
}
If all goes well, [MyJS runJS:js] runs fine and returns a JSON string containing the result of the evaluation of the last statement in the 'js' code.
Now if bad things happen during the evaluation, I get a JSONified exception object. For example, in case of a syntax error in the 'js' code, I get something like this:
{"message":"Parse error","line":1,"sourceId":26121296}
Which is already quite useful to track problems...
However, as I run multiple strings through runJS:, I would like to be able to pinpoint which one caused the exception (because a runtime error could come from a function that was created in a previous javascript code string). This "sourceId" property looks interesting, but I cannot find what it points to. It looks like a pointer address (similar value as pointers to other objects), but it doesn't match with any of the strings I've passed to the evaluator. How can I make this link?
For bonus points: Is there any documentation available about the UIWebView-specific javascript environment, like this exception object? The Safari Web Content Guide is nice, but doesn't go into this kind of details.
Worst-case solution:
Inside each js string being evaluated, add some code that throws an exception, catches it, extracts the sourceId and somehow exposes it so that the runJS: method can keep a record of which sourceId goes with which string.
(Hopefully someone will find a better way and help bury this ugly answer!)

Categories

Resources