evaluate with jurassic js code, which modify html

659 views Asked by At

I'm trying to reimplement getting captcha inside c# application. Having problem, because of no experience in JS.

GET-request returns data, which contains captcha src tag, empty by default.

...
<div class="context"><img src="" id="capchaVisual" alt="loading..."></div>
...

It also returns some javascript, that fills captcha, like this:

function() {
var h = window.location.href;
if (h.indexOf("mytag") + 1) {
    var scr = $("#capchaVisual").attr("src");
    $("#capchaVisual").attr("src", scr + "data:image/jpeg;base64,/9j/MYCAPTCHADECODED...");
}

My steps in c# application:

  1. Load json with data and scripts via WebClient and JavaScriptSerializer. Done. Result stored in jsonobject answer, where answer.data contains html, and answer.e - scripts to execute.
  2. Try to execute loaded scripts via Jurassic:

        var engine = new Jurassic.ScriptEngine();
        engine.EnableDebugging = true;
        //engine.SetGlobalValue("window", answer.data);
    
        foreach (var line in answer.e)
        {
            var evalresult = engine.Evaluate(line);
            string t = evalresult.ToString();
        }
    

    Failed with: ReferenceError: window is not defined.
    If uncomment line 3 engine.SetGlobalValue("window", answer.data), get anotwer error: TypeError: undefined cannot be converted to an object.

1

There are 1 answers

0
Sash0k On

Found an answer in old Jurassic issues. My scripts uses DOM window object, but:

paulbartrum Jan 26, 2011 at 11:01 AM

No, sorry!

Both of those frameworks rely on the existance of a browser DOM. By default, Jurassic can only run "pure javascript" like the CoffeeScript compiler.

Basically if a script does not reference "document" or "window" it will probably work.