0

I display a webpage (not mine) in a webview. There is a Textarea, in which I need to put some text.

<TEXTAREA NAME="app1" ROWS="3" style="width:95%" TABINDEX="1" 
onKeyDown="javascript:return gerTouch(event);"
onKeyUp="javascript:verif(app1,300,true);"
onBlur="javascript:effacer();">TEXT TO CHANGE</TEXTAREA>

Is it possible, with addJavascriptInterface, to rewrite the existing onKeyDown (or another one) function, in order to put my text in the desired place ?

Something like :

webview1.addJavascriptInterface(new Object()
        {
            public void verif(String strl,int i,boolean b)
           {
              //Some code ?
           }
        }, "ok");

The textarea's name changes at each loading, and I cannot access the html textarea with another way. Any other suggestion would be welcome :-) (my goal: put a value in a webview's textarea : Put text inside a textarea in a webview)

Community
  • 1
  • 1
Stéphane GROSSMANN
  • 369
  • 2
  • 5
  • 14

1 Answers1

1

You can change the verif function by running something like after the webview has loaded (call it inside the onPageFinished() of your WebViewClient) :

webview.loadUrl("javascript:window.verif = function(strl,i,b){/*some code*/}");
neomega
  • 712
  • 5
  • 19
  • I put this : webview1.loadUrl("javascript:window.verif = function(strl,i,b){alert(strl);}"); in my onPageFinished, but no display. – Stéphane GROSSMANN Aug 21 '14 at 09:41
  • 1
    @StéphaneGROSSMANN The JS `alert` function does nothing by default in Android webviews, you need to implement the method `onJsAlert` in a `WebChromeClient` to be able to handle alerts (create an AlertDialog with your message for example). – neomega Aug 21 '14 at 09:45
  • Ok thanks, but webview1.loadUrl("javascript:window.verif = function(strl,i,b){strl.value='test';}"); neither does something. – Stéphane GROSSMANN Aug 21 '14 at 09:50
  • @StéphaneGROSSMANN maybe called to soon. You can retard it with an `Handler` field where you have your webview object and doing something like : `handler.post(new Runnable() {public void run() {webView.loadUrl("javascript:...");}});` in your `WebViewClient.onPageFinished` – neomega Aug 21 '14 at 09:56
  • I tryed in Chromes's Console to rewrite the function 'verif', with window.verif=function(champTA, nbCarac, deuxLignes){};, but the old one is always working (deleting text if to much characters) – Stéphane GROSSMANN Aug 21 '14 at 14:53