user801661
user801661

Reputation: 197

Inject JavaScript file in a WebView on Android

I have a function JavaScript that i want to inject in a Html, with the WebView. I can inject small pieces of JavaScript code with loadUrl("javascript:..." but when i try to insert the code does not work.

Code:

<script type="text/javascript" language="javascript">
    var i = 0;

    function timer() {
        tot = document.links.length;
        if (i < tot+1) {
        document.getElementById('link'+i).focus();
            i++;
            setTimeout("timer()", 1000);
        }
    if(i==tot)
        i=0;
    }
    setTimeout("timer()", 1000);

</script>

Upvotes: 2

Views: 4297

Answers (2)

Ads
Ads

Reputation: 6691

Its actually simple, once your webview loading is finshed onPageFinished() , call webview.loadUrl(“javascript:your-javascriptcode-here”) take a look here

Upvotes: 4

JD Brennan
JD Brennan

Reputation: 1112

Maybe something like:

loadUrl("javascript:(function(){" +
  "var i = 0;" +
  "function timer() {" +
  "    tot = document.links.length;" +
  "    if (i < tot+1) {" +
  "    document.getElementById('link'+i).focus();" +
  "        i++;" +
  "        setTimeout('timer()', 1000);" +
  "    }" +
  "if(i==tot)" +
  "    i=0;" +
  "}" +
  "setTimeout('timer()', 1000);" +
"})()");

Upvotes: 0

Related Questions