我正在尝试使用javascript注入更改“font-family”。这是我的代码。
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
myWebView.loadUrl("javascript:(function(){ " +
"css = '@font-face {font-family: \"myface\";src: url(\"file:///android_asset/fonts/DroidSansArmenian.ttf\");}';"+
"css = css + '.opennewstitle { font-family: \"myface\";}';"+
"var head = document.getElementsByTagName('head')[0];"+
"var s = document.createElement('style');"+
"s.setAttribute('type', 'text/css');"+
"s.appendChild(document.createTextNode(css));"+
"head.appendChild(s);"+
"})()");
}
});
String url = "http://m.news.am/arm/news/195853.html";
myWebView.loadUrl(url);
它不起作用,但js代码在Chrome浏览器中运行。