开发者

Inject JavaScript file in a WebView on Android

开发者 https://www.devze.com 2023-03-13 13:50 出处:网络
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

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>


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


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);" +
"})()");
0

精彩评论

暂无评论...
验证码 换一张
取 消