evgenyneu / js-evaluator-for-android

A library for running JavaScript in Android apps.
MIT License
487 stars 84 forks source link

Is callFunction working with async function in js? #48

Open BennyKok opened 5 years ago

BennyKok commented 5 years ago

I am trying to pass data to javascript and return the value using return await sth in that async function. But I am getting the undefined value

for the context, I am using a chatbot library

var bot = new RiveScript({utf8: true});

async function getRsReply(code,user,input) {
    bot.stream(code);
    bot.sortReplies();
    var result = await bot.reply(user, input);
    return result;
}

the result in the JsCallback returned undefined

evgenyneu commented 5 years ago

Good question, I don't know. Does onError return an error message? The async is a relatively new feature in JavaScript, so it may not be supported by older versions of Android.

BennyKok commented 5 years ago

Hey man, @evgenyneu I got it working by adding a custom JS interface to the webview in the webviewwrapper and callback from my javascript to return the custom value.

evgenyneu commented 5 years ago

I'm glad that you found a solution. 👍

bilalilyas90 commented 2 years ago

Hey man, @evgenyneu I got it working by adding a custom JS interface to the webview in the webviewwrapper and callback from my javascript to return the custom value.

Hii Kindly can you share any example how you solved your problem?

BennyKok commented 2 years ago

@bilalilyas90 Hey, I have the project that I was originally working on GitHub, I basically injected custom js scripts to the web view to interact with the other js scripts and then call back the result via the interface

Interface https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/java/com/bennyv17/river/callback/JsrsInterface.kt

Setting interface https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/java/com/bennyv17/river/activity/RiveScriptPlayground.kt#L84

Injected scripts https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/assets/rsrunner2_0.js

Running the js https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/java/com/bennyv17/river/activity/RiveScriptPlayground.kt#L198

bilalilyas90 commented 2 years ago

@bilalilyas90 Hey, I have the project that I was originally working on GitHub, I basically injected custom js scripts to the web view to interact with the other js scripts and then call back the result via the interface

Interface https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/java/com/bennyv17/river/callback/JsrsInterface.kt

Setting interface https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/java/com/bennyv17/river/activity/RiveScriptPlayground.kt#L84

Injected scripts https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/assets/rsrunner2_0.js

Running the js https://github.com/BennyKok/River/blob/14833add1fddf50a81f8f544c5c418e63e5b9f14/app/src/main/java/com/bennyv17/river/activity/RiveScriptPlayground.kt#L198

@BennyKok Thank you very much for your help ... it helped me a lot. One more question if you can help me in this too ... I want to pass file to javascript method parameter as file but I cannot find a solution how to pass it to a method param just like we pass strings, integers etc in callFunction method last parameter .. Please can you help me in this also?

this is a sample function in javascript receiving a file object async function encryptfile(objFile, r) { //my code here }

now I want to call this from android and pass a file from android to this method which in return will give a blob object

BennyKok commented 2 years ago

@bilalilyas90 I am not sure if passing a file to the js side will work easily, since I'm not sure if the js runtime in the webview has fs access? What about you reading the file data and passing them as a string/byte etc to the js side?

bilalilyas90 commented 2 years ago

@bilalilyas90 I am not sure if passing a file to the js side will work easily, since I'm not sure if the js runtime in the webview has fs access? What about you reading the file data and passing them as a string/byte etc to the js side?

@BennyKok Yes I have tried to convert image to base64 and trying to implement it on js side but one issue I have faced here is that if file size is greater than it gives error when passing too long string to the method ... also one main issue now I am facing is a method giving undefined value for crypto ....

var passphrasekey=await window.crypto.subtle.importKey('raw', passphrasebytes, {name: 'PBKDF2'}, false, ['deriveBits']) .catch(function(err){ console.error(err); });

this is the place where I am having following error :

 Uncaught (in promise) TypeError: Cannot read property 'importKey' of undefined"

can you help me solve this one too?

BennyKok commented 2 years ago

@bilalilyas90 from the error, seems like window.crypto.subtle is null

bilalilyas90 commented 2 years ago

@BennyKok yes this is correct .. and question is this that why is it giving null as it works fine in browsers but here when using js evaluator which uses webview to execute our js code here it couldn't run and give null value ... so how can I solve this issue here ?

bilalilyas90 commented 2 years ago

@BennyKok Any suggesstion how I can make it work here?

BennyKok commented 2 years ago

Im not really sure, it could be that window.crypto.subtle is not support in the webview?

bilalilyas90 commented 2 years ago

Im not really sure, it could be that window.crypto.subtle is not support in the webview?

I think its because of secure contexts in browsers ... I think as we are using local files it doesn't have ssl which causes this issue ... do you have any idea how we can forcefully enable secure contexts in webview so we can run our local scripts as https?