Android Question Making a js event on page in webview trigger b4a action?

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I have a page in a webview which should not be visible untill a certain event is fired in the javascript:

B4X:
if (event.type === 'remote') {
var   mediaElement = getMediaElement(event.mediaElement, {
        title: event.userid,
        buttons: [],
        width: width,
        showOnMouseEnter: false
    });

Since there´s no webview incoming event where I could for instance have a text echo´d temporarily and trigger on this, I was thinking that maybe there is a way to do it with javaobject. Sorry if that´s a weird thing to ask that can´t be done at all. Another option is to use advancedwebview where there´s at least a download even that fires whenever a file is being downloaded. I already tried that but with my approach with creating a small file within the script itself, webview (and stock browser) crashes. It may work with a "real" http download, but I find this approach a bit clumsy in the first place, having a file downloading to trigger an event, even if the download is automated.
Let me hear if you got any ideas.

Thanks in advance!
 

DonManfred

Expert
Licensed User
Longtime User
Look at the webview tutorial.
Inform about javascript-interface and Chrome-Client for debugging.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
I'm struggling to understand what you're trying to achieve..

I guess you've looked at webviewextras and webviewextras2?

A little more info would help
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
I'm struggling to understand what you're trying to achieve..

I guess you've looked at webviewextras and webviewextras2?

A little more info would help

Thanks eps,

Sure, let´s try to show how it´d look if webview had a dataarrival event like vb6 has it. This is only theoretical, it won´t work:

B4X:
Dim wview as webview

Sub Activity_Create(FirstTime As Boolean)

wview.visible = false
wview.LoadUrl(url)

End Sub

Sub wview_dataarrival (data as string)

If data.contains ("enablewview") then wview.visible = true

End Sub

And this is the trigger in the javascript:

B4X:
function echo (text)
(..............................}
if (event.type === 'remote') {
echo("enablewview");
};

Is it more clear what I´m trying to achieve now?

PS: Didn´t know there was a webviewextras2 - will check it out!
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Hmm....

So you want to load a webpage (local or remote?) I'm guessing a local one? But basically your html.

You have some javascript which you want to execute before the page is displayed.

Can you not use DOMContentLoaded or similar? If this isn't strict enough you could potentially display a covering page of some sort and then once the other page / event has completed bring it to the fore?

I guess that's the difference between the two technologies.. HTML attempts to showing 'something' as early as possible.

What is the Javascript event doing? Do you need to show the output in a webpage? is it HTML5?

For instance if the javascript is calling a web service, you could call that using OKHTTPUtils first and then waiting for that to complete before then providing the data to the URL and loading the page..?

Still a few too many questions for me - I understand you don't want to give all the info. but the more you give the more we can help and make an informed suggestion.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Hmm....

So you want to load a webpage (local or remote?) I'm guessing a local one? But basically your html.

You have some javascript which you want to execute before the page is displayed.

Can you not use DOMContentLoaded or similar? If this isn't strict enough you could potentially display a covering page of some sort and then once the other page / event has completed bring it to the fore?

I guess that's the difference between the two technologies.. HTML attempts to showing 'something' as early as possible.

What is the Javascript event doing? Do you need to show the output in a webpage? is it HTML5?

For instance if the javascript is calling a web service, you could call that using OKHTTPUtils first and then waiting for that to complete before then providing the data to the URL and loading the page..?

Still a few too many questions for me - I understand you don't want to give all the info. but the more you give the more we can help and make an informed suggestion.


Thanks eps, it´s not THAT secret but I tend to try limiting myself, although I struggle with that sometimes :D
It´s a javascript that loads from a server, but theoretically could also be on the phone. The script sets up a webrtc 1-to-1 chatroom, and when both parties have accepted to join and are connected, it shows the cam. Untill that point, I do not want the page to be visible although it needs to be loaded. So, my problem is to tell b4x, and make b4x understand, that now the action begins so that the webview becomes visible.
I just came up with a good idea - I think: I can simply change the page title when the cams are loading, with something like document.title = "actionaction.";, but is there an event in one of the many webview wrappers that will be able to react on this, something like "sub webview_ontitlechanged"?

I think activating a download is easier than having the script sending a ping that will be seen by a OKHTTPUtils, but thanks for the idea. Also, my getmessages timer only looks every 5 seconds, which could be amended though, so it would take longer than if either a downloadrequest or if I can do it the way I explained above, with an event that listens for a change to the page title.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Can you upload a small project which contains the code to show such a chatroom. Inluding html and js-code....?

Thanks DonManfred (especially you, because your explanations about how to call from java to b4a with webviewextras elsewhere in the forum was what did it for me) and all, but I went studying things a bit, and figured it out!
The page containing the javascript I changed from loading remotely to locally instead, and then opening it with:

B4X:
camview.Loadurl("file:///" & rp.GetSafeDirDefaultExternal("") & "index.html")

Note: B4x tells you that you´ll need to use loadhtml, which didn´t work for me. The forum posts I found told me to use loadurl instead. Also, it tells you to use "file:///file_asset/" to access the local file, which didn´t work for me either. I used the above method instead succesfully.
Then in the sub where the webview is initiated and the html file is loaded, I did not add the view to the activity, but I have this, where camview is the name of the webview (defined in Globals):

B4X:
Dim MyWebViewExtras As WebViewExtras
    MyWebViewExtras.addJavascriptInterface(camview, "B4A")

Next, in the javascript section within the html page that is loaded, I have this in the event that I want to trigger loading the camview, mentioned in the beginning of the thread:

B4X:
B4A.CallSub('javatest', true);

And in b4a:

B4X:
Sub javatest ()
Activity.RemoveAllViews
Activity.AddView(camview,0,0,100%x,100%y)

End Sub

That´s it!
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
So one more experience to add: The webcam wouldn´t load from the script because it´s a local file running it and chrome blocks such requests for security reasons. There are workarounds for this, but they´ll also introduce vulnerabilities, it says. It was my intention anyway to run it from a server, and the good news is that the little B4A.CallSub('javatest', true); part which is crucial for what we´re talking about here also works when it´s run none-locally.
I´d be happy to know though if someone´s aware of a safe way to bypass chromes restrictions for local files requesting cam and mic access.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
So one more experience to add: The webcam wouldn´t load from the script because it´s a local file running it and chrome blocks such requests for security reasons. There are workarounds for this, but they´ll also introduce vulnerabilities, it says. It was my intention anyway to run it from a server, and the good news is that the little B4A.CallSub('javatest', true); part which is crucial for what we´re talking about here also works when it´s run none-locally.
I´d be happy to know though if someone´s aware of a safe way to bypass chromes restrictions for local files requesting cam and mic access.
Not having seen your code, I'm not at all sure of what it is you are trying to do. The one thing you should note is that apps running on Chrome browsers can't access local cameras and microphones unless the application is hosted from localhost or an SSL server (https). Would the lack of a signed SSL certificate be the problem. Chrome will not accept self signed SSL certificates, but I do believe FireFox still does. This may not be helpful, but I thought I would pass it along. Your comments seem to indicate that you have things working using your server.

Regards :)
 
Last edited:
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thanks William, it may be that you´re right but for now I´ll stick to running the javascript remotely, since it works pretty fine.

Take care.
 
Upvote 0
Top