1

I want my webview insert sort of javascript after a page has been loaded. However, the javascript I added has never been executed:( what can I do on this?

mWebView = findViewById(R.id.webview);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished (WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d("WebClient", "onPageFinished");
            view.loadUrl("javascript:(function(){"+
                    "aTagElements=document.getElementsByTagName('a');" +
                    "randromElement=aTagElements[Math.floor(Math.random() * aTagElements.length)];"+
                    "clickEvent=document.createEvent('HTMLEvents');"+
                    "clickEvent.initEvent('click',true,true);"+
                    "randromElement.dispatchEvent(clickEvent);" +
                    "alert('event dispatched.');"+ //<---not called
                    "})()");
        }
});

Even if I comment out all the scripts except the alert line, still nothing happens.

Simon Leung
  • 109
  • 3
  • 14

4 Answers4

2

You will have to use like this on application side

mWebView .addJavascriptInterface(new WebAppInterface(this), "Android");

and will have to implement method on php side where actually this method will be trigger

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

on PHP Side implement like this

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

For a complete reference, you can go through on this link

Whenever you will click the button on webView then you will get a message

ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
1

Try using evaluvateJavascript, loadUrl will work only in old version.

webview.evaluateJavascript("(function() { "javascript:(function(){"+
                "aTagElements=document.getElementsByTagName('a');" +
                "randromElement=aTagElements[Math.floor(Math.random() * aTagElements.length)];"+
                "clickEvent=document.createEvent('HTMLEvents');"+
                "clickEvent.initEvent('click',true,true);"+
                "randromElement.dispatchEvent(clickEvent);" +
                "alert('event dispatched.');";

EvaluvateJavascript

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • it takes 2 parameters and the 2nd one is a value callback. I put a logging in the callback and it shows the log but the javascript (even the alert one) is not executed. – Simon Leung Feb 18 '19 at 05:50
  • did you try it only using alert? – Manoj Perumarath Feb 18 '19 at 06:27
  • 1
    I tried on a simple html made by myself and it works now! I grab up 2 things that mislead me. 1) users cannot see the generated click events occurred 2) alert function works in my simple html, perhaps the alert was blocked by other websites that I thought the script does not work. – Simon Leung Feb 18 '19 at 07:21
1

You are mixing views. So change

super.onPageFinished(view, url);

To

super.onPageFinished(mWebView, url);

statosdotcom
  • 3,109
  • 2
  • 17
  • 40
0

Try the below solution may it works for you.

WebSettings webSettings = webView.getSettings();
webSettings.setDefaultTextEncodingName("utf-8");
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
Mahesh Keshvala
  • 1,349
  • 12
  • 19