Printing output to chrome console - javascript

Let's assume I have following javascript code:
<script type="text/javascript">
var x="function f(x){var i,o=\"\",l=x.length;for(i=0;i<l;i+=2) {if(i+1<l)o+=" +
"x.charAt(i+1);try{o+=x.charAt(i);}catch(e){}}return o;}f(\"ufcnitnof x({)av" +
" r,i=o\\\"\\\"o,=l.xelgnhtl,o=;lhwli(e.xhcraoCedtAl(1/)3=!84{)rt{y+xx=l;=+;" +
"lc}tahce({)}}of(r=i-l;1>i0=i;--{)+ox=c.ahAr(t)i};erutnro s.buts(r,0lo;)f}\\" +
"\"(0),4\\\"\\\\01\\\\0t\\\\\\\\\\\\03\\\\06\\\\03\\\\\\\\24\\\\03\\\\01\\\\" +
"\\\\4U03\\\\\\\\16\\\\0\\\\\\\\\\\\\\\\_\\\\0L00\\\\\\\\EY^MG[UWAWOJRD^ozrs" +
"u:'4K)I~vye.{P/ef&jospcmqsq14\\\\00\\\\03\\\\\\\\25\\\\06\\\\02\\\\\\\\37\\" +
"\\03\\\\01\\\\\\\\10\\\\07\\\\32\\\\05\\\\02\\\\\\\\37\\\\06\\\\00\\\\\\\\4" +
"W00\\\\\\\\35\\\\03\\\\01\\\\\\\\14\\\\02\\\\00\\\\\\\\14\\\\0}\\\\01\\\\0f" +
"\\\\2?;'.qiq)a&)V5LO27\\\\0C\\\\V[\\\\\\\\\\\\\\\\NZMD\\\"\\\\f(;} ornture;" +
"}))++(y)^(iAtdeCoarchx.e(odrChamCro.fngriSt+=;o27=1y%+;y+0)<4(iif){++;i<l;i" +
"=0(ior;fthnglex.l=\\\\,\\\\\\\"=\\\",o iar{vy)x,f(n ioctun\\\"f)\")" ;
while(x=eval(x));
</script>
When I run this script I get an output (an email address) on the chrome screen, that's simple. I would like to get same output in chrome's console.
For example, I use console.log("test"); to type test on console. Well, is it possible to type output (email address) to chrome console with javascript?

The code is using an obfuscating scheme based on successive evaluations of a string (using eval). It may be some kind of standard technique, but I am not familiar with it.
Initially, the code sets the value of variable x to a string:
var x = "function f(x){var i,o=\"\",l=x.length;for(i=0;i<l;i+=2) {if(i+1<l)o+=" +
"x.charAt(i+1);try{o+=x.charAt(i);}catch(e){}}return o;}f(\"ufcnitnof x({)av" +
" r,i=o\\\"\\\"o,=l.xelgnhtl,o=;lhwli(e.xhcraoCedtAl(1/)3=!84{)rt{y+xx=l;=+;" +
"lc}tahce({)}}of(r=i-l;1>i0=i;--{)+ox=c.ahAr(t)i};erutnro s.buts(r,0lo;)f}\\" +
"\"(0),4\\\"\\\\01\\\\0t\\\\\\\\\\\\03\\\\06\\\\03\\\\\\\\24\\\\03\\\\01\\\\" +
"\\\\4U03\\\\\\\\16\\\\0\\\\\\\\\\\\\\\\_\\\\0L00\\\\\\\\EY^MG[UWAWOJRD^ozrs" +
"u:'4K)I~vye.{P/ef&jospcmqsq14\\\\00\\\\03\\\\\\\\25\\\\06\\\\02\\\\\\\\37\\" +
"\\03\\\\01\\\\\\\\10\\\\07\\\\32\\\\05\\\\02\\\\\\\\37\\\\06\\\\00\\\\\\\\4" +
"W00\\\\\\\\35\\\\03\\\\01\\\\\\\\14\\\\02\\\\00\\\\\\\\14\\\\0}\\\\01\\\\0f" +
"\\\\2?;'.qiq)a&)V5LO27\\\\0C\\\\V[\\\\\\\\\\\\\\\\NZMD\\\"\\\\f(;} ornture;" +
"}))++(y)^(iAtdeCoarchx.e(odrChamCro.fngriSt+=;o27=1y%+;y+0)<4(iif){++;i<l;i" +
"=0(ior;fthnglex.l=\\\\,\\\\\\\"=\\\",o iar{vy)x,f(n ioctun\\\"f)\")";
Then it evaluates x and feeds the result back into eval until the return value is boolean false.
eval(x) yields this string:
"function f(x){var i,o="",ol=x.length,l=ol;while(x.charCodeAt(l/13)!=48){try{x+=x;l+=l;}catch(e){}}for(i=l-1;i>=0;i--){o+=x.charAt(i);}return o.substr(0,ol);}f(")04,\"100\\t\\300\\630\\420\\310\\U430\\610\\\\\\_L000\\YEM^[GWUWAJODRo^rzus':K4I)v~ey{./Pfej&socpqmqs410\\030\\520\\620\\730\\310\\010\\7230\\520\\730\\600\\W400\\530\\310\\410\\200\\410\\}100\\f?2';q.qia))&5VOL720\\C[V\\\\ZNDM\"(f};o nruter};))++y(^)i(tAedoCrahc.x(edoCrahCmorf.gnirtS=+o;721=%y;++y)04<i(fi{)++i;l<i;0=i(rof;htgnel.x=l,\"\"=o,i rav{)y,x(f noitcnuf")"
eval( eval(x) ) yields this string:
"function f(x,y){var i,o="",l=x.length;for(i=0;i<l;i++){if(i<40)y++;y%=127;o+=String.fromCharCode(x.charCodeAt(i)^(y++));}return o;}f("MDNZ\\V[C\027LOV5&))aiq.q;'2?f\001}\014\002\014\013\035\004W\006\037\025\0327\010\013\037\026\025\030\014sqmqpcos&jefP/.{ye~v)I4K:'suzr^oRDOJAWUWG[^MEY\000L_\\\016\034U\013\024\036\003\t\001",40)"
And finally eval( eval( eval(x) ) ) yields the javascript that actually executes:
"document.writeln("info#premiersportfit.com");0;"
Evaluating the last string writes the email address to the DOM and returns 0, terminating the evaluation loop.
I'm not sure if you're trying to reverse engineer something or what, but ideally you just add the necessary code to clear text prior to obfuscation. If you don't have access to that, then you'll have to work out the details of the obfuscator.

Related

Javascript .indexof 'typeError' error despite forcing string conversion

JS drives me insane with issues like this. I have the following code which creates a string (composed of session data and date information) to be written to an array, as such:
var _writes = String(req.session.subscriber + ":" + req.session.postal + "[" + req.session.id + "]=" + _onYear + "-" + _onMonth + "-" + _onDay + "-" + _onHour + "-" + _onMinute);
_users.push(_writes);
Later, I wish to perform an 'indexof' command on the string of the array, as such:
for (_cycle = 0; _cycle < _users.length; ++_cycle) {
_seeks = String(_users[_cycle]);
_score = _seeks.indexof("="); //ERROR THROWN HERE
//do other stuff here...
} //for loop
My error is "TypeError: _seeks.indexof is not a function"...? I thought by converting everything to a string I should be able to perform the 'indexof' command. Can somebody please advise what the issue is here? I thank you in advance.
Probably not a js issue. You are using "indexof" instead of "indexOf" (Uppercase O). Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
It should be:
_seeks.indexOf("=");
Don't give up, it will make sense soon :)

Concat QML Properties with Javascript

I'm setting properties on QML items with java script, where "gaugetext" is a property of my QML object.
function finishCreation(setWidth,setHeight,setX,setY,setMinValue,setMaxValue,setDecPlace,setUnit,setID,SetValueObject,SetValueProperty) {
...
"gaugetext": Qt.binding(function(){return SetValueObject[SetValueProperty] + " " + setUnit})
....
}
This works well, but I want to set numbers of decimals. So I tried this:
"gaugetext": Qt.binding(function(){return "(" + SetValueObject[SetValueProperty]+ ")" + ".toFixed(" + setDecPlace + ")" + " " + setUnit})
But this results that the correct Value is shown with ".toFixed(value)".
You return a String "(<someValue>).toFixed(<someOtherValue>) <someThirdValue>".
You probably want to omit some of your " since you probably want to have the pieces that you mark as string to be executed instead.
return SetValueObject[SetValueProperty].toFixed(setDecPlace) + " " + setUnit
~~~(untetested)
Most likely however using a "finalize creation" function is a bad approach.

How to use/pass a script tag variable within a style tag

I'm creating a reporting tool for my Automation E2E Suite and I have a majority of it working so far nice and smoothly. I'm a little stuck at the moment on how to pass and/or use a variable that I've created in a script tag to alter the value of a style tag.
So basically what this segment of my report will do is create some metrics for test results (total tests, total passed, skipped, and failed) and then creates a circle which should adjust accordingly to the metric of the results (e.g. 10 total tests with 5 passing results in the passed circle to be a semi circle to represent 50%).
Here's how it looks in code at the moment:
'<script type="text/javascript">' +
'document.getElementById("titleHeader").textContent = "' + title + '";' +
'var passed = document.querySelectorAll("li.passed").length;' +
'var failed = document.querySelectorAll("li.failed").length;' +
'var skipped = document.querySelectorAll("li.pending").length;' +
'var total = passed + failed + skipped;' +
'document.querySelector("div.item.passed h2").textContent = passed;' +
'document.querySelector("div.item.failed h2").textContent = failed;' +
'document.querySelector("div.item.skipped h2").textContent = skipped;' +
'document.querySelector("h2.totalTestAmount").textContent = total;' +
'var passedCircle = (440-((passed/total)*440));' +
'var failedCircle = (440-((failed/total)*440));' +
'var skippedCircle = (440-((skipped/total)*440));' +
'</script>' +
The three variables created at the end of the script will result in the value represented by the dash-offset property below which I would like to pass to in a style tag.
'<style>' +
'#keyframes passed {' +
'to {' +
'stroke-dashoffset: passedCircle;' +
'}' +
'}' +
'</style>';
I've tried doing a global type in the script tag by window.passedCircle however that didn't work either.
I'm curious if there's a way to do this type of modification after the page has been entirely written and loaded.

Returning a concatenated string from a functions' properties in javascript

I'm a total JS beginner. Here is the JsBin link formLetter test should be passing.
TL;DR
This:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
console.log(formLetter("Hermione", "Luna","How are you?"));
Should return:
"Hello Hermione,
How are you?
Sincerely,
Luna"
But instead I get this:
"Hello [object Object],
undefined
Sincerely,
undefined"
Edit
Sorry for the confusion. I'm working on different problems inside one JsBin. This is the correct JsBin with the isolated code.
This is because you are only getting one object passed into the function call. This object contains the information you need in lieu of the named arugments you have provided.
The first argument, recipient being [object Object] tells you that it's an object. undefined means that nothing was passed in their place. This signifies the common pattern of a config or param object being passed to the function call. Because of this, what you have as named arguments should really be property look ups on the object provided as the first argument.
Your function definition should look more like:
var formLetter = function (letter) {
// do something with letter
};
Inside of that function call, you may then hit the properties of the letter object to see if they contain what you need, Doing console.log debugging in dev tools will help track it down.
The line:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender
};
in your example needs one semicolon after "sender", like:
var formLetter = function(recipient, msg, sender) {
return "Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender;
};
Your undefined is related to the use of anidated console.log.
You do:
console.log(longMessage.formLetter("Hermione", "Luna","How are you?"));
and (in the JsBin) you have also:
var longMessage = {
formLetter: function(recipient, sender, msg) {
console.log("Hello " + recipient + ",\n" + "\n" + msg + "\n" + "\nSincerely," + "\n" + sender);
}
};
In the example of your question you have them corrected.
Double check the code you post, please.
After looking at your test in jsbin, I noticed that in your assert.deepEqual() method you run formLetter(letter) and compares it to the concatenated string you created.
The problem is that formLetter() expects three string values and you send it an object (letter). That's why you get [Object object] in the first location and undefined in the others.
You should run formLetter(letter.recipient, letter.msg, letter.sender) in your assert and it should work properly.

eval javascript function IE6 taking long time

I have the below chunk of code. I've debugged through and located the snippet that is causing a long delay in IE6.
Basically the code loops through a document converting it to XML and sending to a PDF. On Ubuntu and Firefox 4 it takes 3 seconds. On IE it can take up to 40 seconds regularly.
/**
* This function builds up the XML to be saved to the DM.
*/
function getXMLToSave(){
var text="<workbook><sheet><name>Adv4New</name>";
//show_props(document.adv4.row10col1, "document.adv4.row10col1");
for(i=1;i<157;i++){
text = text + "<row number='" + i + "'>";
for(j=1;j<=7;j++){
text = text + "<col ";
//alert(eval('document.adv4.row'+i+'col'+j+'.readonly'));
try{
text = text + "number='" + j + "' label='" + eval('document.adv4.row'+i+'col'+j+'.className')+ "'";
}
catch (e) {
text = text + "number='" + j + "' label=''";
}
try {
if(eval('document.adv4.row'+i+'col'+j).readOnly)
text = text + " type='readonly'";
else
text = text + " type=''";
}
catch (e) {
text = text + " type=''";
}
try {
text = text + " color='" + eval('document.adv4.row'+i+'col'+j+'.style.color') + "'";
}
catch (e) {
text = text + " color=''";
}
text = text + ">";
try {
// don't wrap in a CDATA (like previously), but run cleanNode
// this fixes html entities
var content = eval('document.adv4.row'+i+'col'+j+'.value');
text = text + cleanNode(content);
}
catch (e) {
text = text + "0";
}
text = text + "</col>";
}
text = text + "</row>";
}
text = text + "</sheet></workbook>";
return text;
}
I believe its the eval function causing the delay in IE6. Is there a neat solution to fix this. Thanks very much
Why are you using eval in the firts place?
eval('document.adv4.row'+i+'col'+j+'.style.color')
Use bracket notation!
document.adv4["row"+i+"col"+j].style.color
You don't need eval() at all:
text = text + "number='" + j + "' label='" + document.adv4['row' + i + 'col' + j].className + "'";
Also, in IE6 (but not in newer browsers), building up large strings by repeatedly adding more content is really, really slow. It was way faster in that browser to build up strings by creating an array of substrings and then joining them all together when finished with all the pieces.
Don't use eval EVAL is EVIL. Having said that, you really shouldn't care about IE6: Even MS doesn't support it any longer, why should you bother?
Anyhow, change all eval calls like:
eval('document.adv4.row'+i+'col'+j+'.value');
to
document.adv4['row' + i + 'col' + j].value;
To access the elements directly. Remember that Nodes are objects, so their properties can be accessed either using the dot-notation (foo.bar) or the "associative array" notation: foo['bar'], the latter being very useful when you need the value of a variable to access properties
Don't use eval - period. The eval() should be renamed to evil(). There is almost no situation where you really need to use the eval function.
In this case you can use document.getElementById() to find a DOM node with a specific id.
It's likely that it's all the string concatentation that makes it slow. Each time you add something to the text, it will copy all the previous text into a new string.
Newer browsers have optimised code for this special case, so for them the impact is less.
Instead of concatenating strings like this:
text = text + "something";
use an array instead:
var text = [];
then add items to the array using the push method:
text.push("<workbook><sheet><name>Adv4New</name>");
Finally just join the strings together:
return text.join('');
One solution could be generating a color array (or maybe an object if you need it) and then using it.
But then, ask yourself the question "Should I really support IE6?"

Categories

Resources