Android webview 加载H5时,隐藏掉不需要的元素

h5加载完成时回调
override fun onPageFinished(view: WebView?, url: String?) {
    super.onPageFinished(view, url)
    view?.loadUrl("javascript:" + loadJs() + ";")
}
fun loadJs(): String {
    var js = "var newscript = document.createElement(\"script\");"
    js += "document.getElementsByClassName('ads-head')[0].style.display='none';"
    js += "document.getElementsByClassName('ads-cont')[0].style.display='none';"
    js += "document.getElementsByClassName('ads-foot')[0].style.display='none';"

    js += "document.getElementById('ads_tw').style.display='none';"
    js += "document.getElementById('pi').style.display='none';"
    // 通过控件id,直接获取元素

    js += "document.querySelectorAll('.container>div')[4].style.display='none';"
     //表示类container下的child div,然后取第四个孩子

    js += "document.querySelectorAll('.cont>div')[3].style.display='none';"

    js += "document.getElementsByClassName('list')[3].style.display='none';"

    js += "document.getElementsByClassName('ads-nav')[0].style.display='none';"
    //表示控件用的class ,然后取控件

    js += "document.getElementsByClassName('ads-bot')[0].style.display='none';"
    js += "document.getElementsByClassName('headbar')[0].style.display='none';"
    js += "document.body.appendChild(newscript);"
    return js
}

. 表示引用class

# 表示引用id,找控件

你可能感兴趣的:(Android)