Get javascript callback from webview - javascript

I'm trying to get a json data and a callback from javascript using webview.
I can get the json data but the problem is I cannot get the callback.
I need to fire the callback after a condition is met.
Android Code:
--------
// Adding the interface
webView.addJavascriptInterface(new WebAppInterface(this), "code");
--------
#JavascriptInterface
public void execute(String JsonData, String callback) {
String d = data; <---HAS JSON DATA RETURNED TO ANDROID
Log.d("jSon Data", d);
mCallback = callback; <---RETURNS "undefined"
}
Javascript code (I can't edit this.):
code.execute(JsonData, function(callback){
console.log(callback);
});
Android on button click
#OnClick(R.id.callback)
void onButtonCallback() {
String s = "Hello World";
// pass Hello World back to javascript. But I'm getting "undefined"
// for the callback
mCallback.passdata(s);
}
What I'm trying to achieve is:
1) Get data from Javascript to Android -> OKAY
2) Get the Callback from Javascript to Android -> Here's my problem
3) Fire the callback along with "hello world" string on button click
Note: I can't edit the javascript code. How am I going to achieve this? Do I need to inject Js from Android? If yes, how?
I just need to solve item number 2 to move forward. Thanks!

You can't do that. At least, based on my tests addJavascriptInterface() only works with primitive types and Strings, and so you cannot pass Javascript objects like functions.

Related

Binding javascript code to android code with objects parameters

As the title suggests, I'm trying to bind javascript code to my android app so I can react in my app to an event/message that my website is sending.
After reading the official android documentation related to javascript binding I managed to easily implement it.. as long as it's a string.
What is working fine?
I implemented the following code in my app:
/** Instantiate the interface and set the context */
class ClientInterface(private val mContext: Context) {
/** Show a toast from the web page */
#JavascriptInterface
fun postMessage(message: String) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show()
}
}
if the parameter of the 'postMessage' function is a String and I'm passing a String from my javascript as a parameter, everything is fine. it's passing the string.
my problem is that I am trying to get a JSONObject instead of a String, and it's not working.
I tried casting everything I thought might work.. JSONObject / JSONObject? / Any / Any? / Object / Object? and so on..
when I'm sending an object on my javascript, nothing seems to work. all I get in my app is a null response.
anyone ever tried something like that? what am I missing?
P.S - here's my javascript code for reference:
var objectMessage = {
type: "quote",
code: "My name is Inigo Montoya. You killed my father, prepare to die!"
}
window.CLIENT.postMessage(objectMessage);
You can't pass an object only primitive!
So you need to stringify your object.
var objectMessage = {
type: "quote",
code: "My name is Inigo Montoya. You killed my father, prepare to die!"
}
window.CLIENT.postMessage(JSON.stringify(objectMessage));

pass string from C# to Javascript with Phonegap and Windows Phone 8

When developing a plugin, how is possible to get the result of my C# function to my javascript.
For instance having the echo plugin as in the official cordova doc
When I pass a result from C# to Javascript calling
DispatchCommandResult(new PluginResult(PluginResult.Status.OK, "{result:\"super awesome!\"}"));
Please How can I get the result inside my javascript code (how can I get: "super awesome")?
When you dispacth the plugin result with status OK from C#, the win callback is called, and it brigns the result with this.
Example, your javascript plugin calls this from javascript:
cordova.exec(win, fail, "Echo", "echo", ["input string"]);
So you have to create a win function that will get the result:
function win(result) {
alert(result);
}
You could just write the string directly :
<script> var JavascriptBlah = '<%=yourString%>'</script>
or use the method InvokeScript to call a function to set the string :
webBrowser.InvokeScript("yourStringSetter", "yourString");
demo of a simple setter in js
source for the first solution

devexpress callback results syntax

I am trying to figure out the correct javascript syntax for a DevExpress datagrid callback to pass data back to the client.
In the .aspx I installed an onclick event in the DataGrid row with a CustomCallback event using the js call: dg.PerformCallback(key); and in the aspx.cs file this function is correctly reached, however I cannot pass data back to the client:
protected void dg_CustomCallback(
object sender,
DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
{
string key = e.Parameters; // works
e.Results = "something"; // .Results does not exist
return;
}
Then I switched from a CustomCallback to a DataCallback because the DevExpress.Web.ASPxGridView.ASPxGridViewCustomDataCallbackEventArgs does have a .Results property. However, I cannot figure out the corresponding javascript call. I tried in vain: dg.PerformCallback(key); , dg.PerformDataCallback(key); and dg.SendCallback(key);
Also I am wondering, when the above problem is fixed, which js function I need to program to receive the return data from the server after the callback.
What you are trying to achieve can be done using the JSProperties on callback and the OnEndCallback client side event of the ASPxGridview. This aspx tag can be placed exactly after the </Columns> closing tag of the ASPxGridview.
<ClientSideEvents EndCallback="function(s,e)
{
var errText = s.cpError;
if (errText != "")
{
alert(errText);
}
}" />
On the server side you set the JSProperties like this
gridOfApp.JSProperties["cpError"] = "The error was major!";
Important. Bear in mind that your JSProperties MUST ALWAYS start with the cp prefix.

CHtmlView: How to get data on click?

I have an MFC application that uses CHtmlView. It displays some text in html format from some temp html file. Is it possible to handle mouse click on a paragraph to send some data to the program? I understand that javascript can be used to handle click, but how to pass the data from javascript function to the application??
Thanks.
It is possible to cleanly call the containing application from within the Javascript of the HTML page. At the Javascript level the MSHTML interface that is doing the actual work of the CHtmlView provides an "external" object that acts as a way back to the calling application.
Suppose we want to add a method "someCall()" that can be called from Javascript, and that the method takes a string as an argument. In JavaScript we would call it with something like
external.someCall("An example string");
In the MFC application, we need to write a CCmdTarget derived object to act as the implementation of the "external" object as a dispatch-based COM object, something like:
class TestExternal : public CCmdTarget
{
public:
TestExternal()
{
EnableAutomation();
}
void SomeCall(LPCWSTR str)
{
// This is where we get called when the Javascript runs...
}
private:
DECLARE_DISPATCH_MAP()
};
BEGIN_DISPATCH_MAP(TestExternal,CCmdTarget)
DISP_FUNCTION(TestExternal,"someCall",SomeCall,VT_EMPTY,VTS_WBSTR)
END_DISPATCH_MAP()
To tie this implementation of "external" with the HTML view, in a class derived from CHtmlView you need to over-ride OnGetExternal() and to point it to an instance of TestExternal that lives at least as long as the CHtmlView:
class TestHtmlView : public CHtmlView
{
// Usual implementation stuff goes here...
public:
HRESULT OnGetExternal(LPDISPATCH *lppDispatch)
{
*lppDispatch = m_external.GetIDispatch(TRUE);
return S_OK;
}
private:
TestExternal m_external;
};
Note that I haven't actually tested this, but it seems about right from memory ...

Call JavaScript function from Silverlight 4.0 application

I am trying to call a function from a Silverlight application. It should be a very simple task to do but so far I am not getting the result that I am looking for.
This is my Silverlight code:
private void button2_Click(object sender, RoutedEventArgs e)
{
HtmlPage.Window.Invoke("SayHello", new string[] { "Salut!" });
}
And this is the JavaScript code :
function SayHello(theid) {
alert(eval(theid));
var divStatusDiv = document.getElementById("divStatus");
divStatusDiv.style.backgroundColor = "Red";
}
The alert message always show "undefined" but when I press "OK" the colour of that DIV gets changed to Red as it should be.
Why am I getting "Undefined" all the time ?
You need to create the json that can be passed properly instead of just passing along an array like that. You can simply return "Salut!" instead of new string[] { "Salut!" } or you can create the json array for the string array you have.
I'm not familiar with Silverlight, but if theid has value "Salut!" inside of SayHello, then you cannot eval it, since it is a string of text, not code. You should change the line alert(eval(theid)); to just alert(theid);.
Use
alert(eval(theid.value));

Categories