Webview动态注入js脚本
/** 要注入的js代码 function(){ var objs = document.getElementsByTagName('img'); for(var i = 0; i <objs.length; i++) { objs[i].onclick = function() {window.toolbox.openImage(i,this.src); } }; **/ // 注入js函数监听 public void injectImgClick() { mWebView.loadUrl("javascript:(function(){var objs = document.getElementsByTagName('img');for(var i = 0; i <objs.length; i++) {objs[i].onclick = function() {window.toolbox.openImage(i,this.src);};}})()"); }
webview.getSettings().setJavaScriptEnabled(true); webview.addJavascriptInterface(new Object(){ @JavascriptInterface public void openImage(int i, String src) { ArrayList<String> list = new ArrayList<String>(); list.add(src); mActivity.startActivity(new Intent(mActivity, PhotoViewActivity.class).putExtra(PhotoViewActivity.EXTRA_PHOTOS, list) .putExtra(PhotoViewActivity.EXTRA_TYPE, PhotoViewActivity.TYPE_VIEW)); } }, "toolbox");
webview.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); injectImgClick(); } });
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。