注入装上的WebView上everypage一个JavaScript API [英] Inject a JavaScript API on everypage loaded on WebView

查看:125
本文介绍了注入装上的WebView上everypage一个JavaScript API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码一个离线SCORM的球员,所以需要由的WebView实例加载每个HTML页面上的注入的JavaScript API。
我试图用一个框架集的方法,但是WebView中犯规行为,应该(除此之外,其德$ P $在HTML5 pcated)的方式。
我怎样才能做到这一点?我需要的脚本页面加载之前被注入,因为那些html页面会消耗身体上onLoad事件的API。

I am coding a offline Scorm player, so need to inject an JavaScript API on every html page loaded by the WebView instance. I tried to use a frame-set approach, but WebView doesnt behave the way it should (besides that, its deprecated in HTML5). How can I achieve this? I need the script to be injected before the page loads, because those html pages will consume the API on the body onLoad event..

当试图重写onPageStartedWebViewClient方法,虽然事件被触发时,JS code注射无法达成。

When trying to to override the 'onPageStarted' WebViewClient method, although the event was fired, the JS code injected could not be reached.

在此先感谢,
                巴勃罗

Thanks in advance, Pablo

推荐答案

您可以通过以下code注入的JavaScript everpageload,希望它能帮助

you can inject javascript everpageload by using below code, hope it helps

final WebView webview = (WebView)findViewById(R.id.webView1);  
        /* JavaScript must be enabled if you want it to work, obviously */  
        webview.getSettings().setJavaScriptEnabled(true);  

        /* WebViewClient must be set BEFORE calling loadUrl! */  
        webview.setWebViewClient(new WebViewClient() {  
            @Override  
            public void onPageFinished(WebView view, String url)  
            {  

                webview.loadUrl("javascript:myFunction()");  
            }  
        });  

        webview.loadUrl("http://code.google.com/android"); 

HTML 

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
   alert("Hello World!");
}
</script>
</head>

<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>

这篇关于注入装上的WebView上everypage一个JavaScript API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆