How can I change the “font-family” css style inside WebView after html is loaded

随声附和 提交于 2019-12-23 02:45:22

问题


I'm trying to change "font-family" with javascript injection. Here is my code.

        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);

It doesn't work, but the js code works inside Chrome browser.

来源:https://stackoverflow.com/questions/22015672/how-can-i-change-the-font-family-css-style-inside-webview-after-html-is-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!