How can I call a javascript function using C# in Content Grabber?

386 views Asked by At

I am trying to call a Javascript function using C# in Content Grabber. (Content Grabber is a web scraping software).

The Javascript code is this:

$.definePage({
    idRecaptcha: null,

    init: function() {},

    carregarReCaptcha: function() {
        if(page.idRecaptcha == null) {
            var sitekey = $("#reCaptchaPublicKey").val();
            page.idRecaptcha = grecaptcha.render($("#tecRecaptcha")[0], {
                'callback' :  page.verifyCallback,
                'sitekey': sitekey
            });
        }
    },

    verifyCallback: function(response) {
        if(response) {
            $("#form").submit();
        }
    }
});

var onloadCallback = function() {
    page.carregarReCaptcha();
}

The function I want to call is "verifyCallback". This function essentially submits the recaptcha token, which will verify if the token I have entered is correct or not.

In my Content Grabber agent, I want to call this function, and I have this code, but it's giving me an error:

using System;
using System.Web.UI;
using Sequentum.ContentGrabber.Api;
public class Script
{
    //See help for a definition of CustomScriptArguments.
    public static CustomScriptReturn CustomScript(CustomScriptArguments args)
    {
        // retrieve page from current handler
        var page = System.Web.HttpContext.Current.CurrentHandler as Page;

        if (page == null)
        {
            // do something, e.g. throw exception
            return CustomScriptReturn.Pause();
        }

        // Place your script code here.
        // Return empty for no special action.
        string response = args.DataRow["Captcha"];
        string script = "page.verifyCallback('" + response + "');";

        // call ClientScript from existing page instance
        page.ClientScript.RegisterStartupScript(page.GetType(), "page.verifyCallback", script, true);

        return CustomScriptReturn.Empty();
    }
}

When I compile it, it's returning this error:

Object reference not set to an instance of an object.

It looks like I can't just delete object sender, EventArgs e

I'm not really familiar with JS or C#, so I would appreciate any help I can get. Thank you so much!

2

There are 2 answers

8
Tetsuya Yamamoto On

The problem occurs because you're trying to use ClientScript instance from a class which not inherited from System.Web.UI.Page (base class for code-behind pages). As long as you have access to HttpContext.Current, you can retrieve Page instance from its handler property (i.e. CurrentHandler) and use ClientScript as in example below:

public class Script
{
    //See help for a definition of CustomScriptArguments.
    public CustomScriptReturn CustomScript(CustomScriptArguments args, object sender, EventArgs e)
    {
        // retrieve page from current handler
        var page = HttpContext.Current.CurrentHandler as Page;

        if (page == null)
        {
            // do something, e.g. throw exception
        }

        // Place your script code here.
        // Return empty for no special action.
        string response = args.DataRow["Token"];
        string script = "verifyCallback('" + response + "');";

        // call ClientScript from existing page instance
        page.ClientScript.RegisterStartupScript(page.GetType(), "verifyCallback", script, true);
        return CustomScriptReturn.Empty();
    }
}

Update:

As for explaining the second error after edit, it occurred because you're declaring a method named callback inside CustomScript method, which is not valid (and the return statement must be on the last). If the sender and EventArgs handlers are unnecessary then simply omit them. Here is an example to properly return CustomScriptReturn:

public static CustomScriptReturn CustomScript(CustomScriptArguments args)
{
    // retrieve page from current handler
    var page = System.Web.HttpContext.Current.CurrentHandler as Page;

    if (page == null)
    {
        // do something, e.g. throw exception
    }

    // Place your script code here.
    // Return empty for no special action.
    string response = args.DataRow["Captcha"];
    string script = "page.verifyCallback('" + response + "');";

    // call ClientScript from existing page instance
    page.ClientScript.RegisterStartupScript(page.GetType(), "page.verifyCallback", script, true);

    return CustomScriptReturn.Empty();
}

Related issue:

An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get'

1
JTech On

In addition to @Tetsuya's answer, it looks like you'll have an issue with the Javascript you're passing to RegisterStartupScript.

I don't think you'll be able to just call "verifyCallback" because that is a function defined within the scope of the $.definePage({}) call.

So you'll need to dig deeper into that and find out if the verifyCallback function is exposed publically.

Based on this code:

var onloadCallback = function() {
    page.carregarReCaptcha();
}

I'd say you'd need to call:

page.ClientScript.RegisterStartupScript(page.GetType(), "page.verifyCallback", script, true);