Javascript string oddities

I was trying to scrape a page and wondering why content on the page wasn't in the page source. It turns out that the content was encoded as gibberish and decoded on the fly with javascript.

i.e. document.write(rot_decode('gibberish'))

I downloaded the JS file and found the function. Not having used JS before I thought I'll just make a JS script with the function in it and call from my normal intepreted java script. In my intepreted java script I've got the following function:

String rot_decode(String decodeString) {
session.setVariable("DECODE_STRING",decodeString);
session.executeScript("yu au s -1.1 rot_decode");
return session.getVariable("DECODED_STRING");
}

The JS script is as follows:

function rot_decode(encodedString){
    var decodedString = "";
    session.logInfo("len:" + encodedString.length);
      
    for (i = 0; i < encodedString.length; i++) {
        var character =  encodedString.charAt(i);
       session.logInfo("chars:");
       session.logInfo(character);
      
        if(/[a-zA-Z]/.test(character)){
           var base =(/[A-Z]/.test(character))? 'A':'a' ;
           decodedString += String.fromCharCode((character.charCodeAt(0) - base.charCodeAt(0) + 39 ) %26 + base.charCodeAt(0));
        } else if (/[0-9]/.test(character)){
           decodedString += String.fromCharCode((character.charCodeAt(0) - 48 + 15 ) %10 + 48);
        } else{
           decodedString += character;
        }
    }
    session.logWarn(encodedString);
    session.logWarn(decodedString);
    return decodedString;                 
}

//var decodable = session.getVariable("DECODE_STRING").toCharArray();
var decodable = session.getVariable("DECODE_STRING").toString();
var decoded = '';
if (decodable != null)
decoded = rot_decode(decodable);
session.setVariable("DECODED_STRING",decoded);

session.logError("decodable1: " + decodable);
session.logError("decoded1: " + decoded);
session.logError(rot_decode(decodable));

//session.logError(rot_decode('<qvi pynff="pryy5"><u7>Nqqerff</u7><c>7729 Zbttvyy Eq<oe />Xrazber<oe />DYQ, 9514<oe />Nhfgenyvn<oe /><n gvgyr="Qrgnvyrq znc naq qverpgvbaf gb Unzcgba Tneqraf Ahefrel" pynff="yvaxf" uers="/bayvarFbyhgvba_erqverpg.qb?rzy=gehr&fgr=DYQ&urnqvatPbqr=72074&gkgQ=Orfg+Dhnyvgl+Cebqhpgf%7P+Freivpr+%71+Nqivpr+Sbe+Tneqravat+Arrqf.+&fgAoe=7729&ybp=Xrazber&cebqhpgIrefvba=6&fgGc=Eq&cebqhpgVq=605829&voyVq=7193585&jroFvgr=uggc%8N%7S%7Sjjj.unzcgbatneqraf.pbz.nh&cp=9514&cg=zq&cntrAhzore=8&cuAoe=%7352%74+8823+0008&fgAz=Zbttvyy&fg=pf&m=&cyAz=Unzcgba+Tneqraf+Ahefrel" anzr="zncYvaxCnenzf">Trg qverpgvbaf</n></qvi>'));
//session.logError(rot_decode('<qvi vq="cubarAhzoref" pynff="fcnaEbj"><u7>Pbagnpg Qrgnvyf</u7><qy vq="pbagnpgQrgnvyf"><qg>Cu:</qg><qq>(52) 8823 0008</qq><qg>Snk:</qg><qq>(52) 8323 4741</qq><qg>Bgure:</qg><qq>(52) 8823 0008</qq></qy></qvi>'));

The problem is, it doesn't seem to like taking the String it gets from session.getVariable("DECODE_STRING"). You can see the commented out sections at the end where I'm calling the function with the string literally. That works fine and displays the decoded content. But when the string comes via the session variable it returns a blank string (not a null string).

I think maybe because the string is coming from a Java object it might be treating it as Java string rather than the JS version. The loop inside the decode function is not executing at all so I suppose the encodedString.length function is not actually returning a number. In fact session.logInfo("len:" + encodedString.length); actually shows: "len:function length() {/* int length() */}"

so I think I need to somehow cast the Java string to whatever the JS datatype is for strings. But that's about where I've reached the end of my tether. I can't for the life of me figure out how to do it.

help! sanity is rapidly running off into the wild blue yonder...

4 hours of pulling my hair

4 hours of pulling my hair then 5 minutes after I write this I figure it out... needed to replace:

var decodable = session.getVariable("DECODE_STRING").toString();

with

var decodable = String(session.getVariable("DECODE_STRING").toString());