앱에서만 로드되어야 하는 js, css가 있어서

WebViewClient의 onPageFinished 시점에 js 를 embed 시키기 위해 코드를 작성하였다.



public void injectCss(WebView wv, String link){
    if(!"".equalsIgnoreCase(link)){
        try{
            wv.loadUrl(
                    "javascript:(function() { " +
                            "var css = document.createElement('link'); " +
                            "css.rel = 'stylesheet'; " +
                            "css.type = 'text/css'; " +
                            "css.href = '" + link + "'; " +
                            "document.getElementsByTagName('head')[0].appendChild(css)" +
                            "})()"
            );
        }catch (Exception e){}
    }
}

public void injectJs(WebView wv, String link){
    if(!"".equalsIgnoreCase(link)){
        try{
            wv.loadUrl(
                    "javascript:(function() { " +
                            "var js = document.createElement('script'); " +
                            "js.type = 'text/javascript'; " +
                            "js.src = '" + link + "'; " +
                            "document.getElementsByTagName('body')[0].appendChild(js)" +
                            "})()"
            );
        }catch (Exception e){}
    }
}


onPageFinished 시점 위 함수를 호출해주면 해당 link의 js나 css를 embed 시켜주는 걸 확인 할 수 있다.

( 크롬 인스펙터로 확인하면된다 )

+ Recent posts