JavaScript async callback

Add-in Express™ Support Service
That's what is more important than anything else

JavaScript async callback
Call a C# method from JavaScript with callback 
Alexandre Swioklo




Posts: 3
Joined: 2015-02-07
Hi,

Im trying to create an async call from JavaScript to BHO, but Im having problems to find the context to call execScript after my task ends. Could you help me?


private void IEModule_DocumentComplete(object pDisp, string url) {
    try {
        var scriptEngine = HTMLDocument.Script;
        if (scriptEngine != null) {
            scriptEngine.GetType().InvokeMember("javascriptObj", BindingFlags.SetProperty, null, scriptEngine, new object[] { this });
        }
    } catch {
    }
}

public string ListNames(string callBack) {
    var id = Guid.NewGuid().ToString();
    var ret = String.Empty;
    Util.Trace("ListNames Id:{0}", id);
    Task.Factory.StartNew(() => {
        try {
            Util.Trace("ListNames Task started Id:{0}", id);
            var names = new List<String>() {"Name 1","Name 2","Name 3"};
            var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var resultJson = JsonConvert.SerializeObject(names, Formatting.None, jsonSerializerSettings);
            ret = String.Format("{0}('{1}','{2}')", callBack, resultJson, id);
            Util.Trace("ListNames Task result Id:{0} callBack:{1}", id, ret);
        } catch (Exception ex) {
            Util.Log("An error has occurred while processing a command: " + ex);
        }
    }).ContinueWith(result => {
        try {
        Util.Trace("ListNames Task ContinueWith result Id:{0} callBack:{1}", id, ret);
        HTMLDocument.parentWindow.execScript(ret, "JScript");
        } catch (Exception ex) {
            Util.Log("An error has occurred while processing a command: " + ex);
        }
    }, TaskScheduler.Current);
    return id;
}


    }



Thanks,

Alexandre
Posted 07 Feb, 2015 08:55:06 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Alexandre,

Can you please clarify the question? What exactly context do you mean?
Posted 09 Feb, 2015 09:39:03 Top
Alexandre Swioklo




Posts: 3
Joined: 2015-02-07
Hi Sergey,

I want to call a C# function from JavaScript asynchronously. The C# function receives a parameter with the name of the JavaScript function to be called when it finish.

In JavaScript code I want to call javascriptObj.ListNames('callbackFunction'); and in c# I call back the callbackFunction



var javascriptObj = null;
function callbackFunction() {
	alert("Test");
}

function MyFunction() {
	if (javascriptObj != null) {
		var id = javascriptObj.ListNames('callbackFunction');
	} else
		alert("The add-on isn't registered");
}
Posted 09 Feb, 2015 18:18:22 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Alexandre,

In your code, I see 'HTMLDocument.parentWindow.execScript(ret, "JScript");'. Probably the correct code will be 'HTMLDocument.parentWindow.execScript(callBack, "JScript");'.
Posted 10 Feb, 2015 05:11:31 Top
Alexandre Swioklo




Posts: 3
Joined: 2015-02-07
Hi Sergey,

But in C# how can I call back the callBack function?

I have tried to use this code, but it do not work.

public string ListNames(string callBack) { 
    var id = Guid.NewGuid().ToString(); 
    var ret = String.Empty; 
    Util.Trace("ListNames Id:{0}", id); 
    Task.Factory.StartNew(() => { 
        try { 
            Util.Trace("ListNames Task started Id:{0}", id); 
            var names = new List<String>() {"Name 1","Name 2","Name 3"}; 
            var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; 
            var resultJson = JsonConvert.SerializeObject(names, Formatting.None, jsonSerializerSettings); 
            ret = String.Format("{0}('{1}','{2}')", callBack, resultJson, id); 
            Util.Trace("ListNames Task result Id:{0} callBack:{1}", id, ret); 
        } catch (Exception ex) { 
            Util.Log("An error has occurred while processing a command: " + ex); 
        } 
    }).ContinueWith(result => { 
        try { 
        Util.Trace("ListNames Task ContinueWith result Id:{0} callBack:{1}", id, ret); 
        HTMLDocument.parentWindow.execScript(ret, "JScript"); 
        } catch (Exception ex) { 
            Util.Log("An error has occurred while processing a command: " + ex); 
        } 
    }, TaskScheduler.Current); 
    return id; 
} 
Posted 10 Feb, 2015 05:30:14 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Please try to use '()' after the function's name as shown below.

HTMLDocument.parentWindow.execScript("callbackFunction()", "JScript");
Posted 10 Feb, 2015 06:06:01 Top