如何删除 Android WebView 中的 header? (加载时)
How to remove the header in Android WebView? (While Loading)
我想删除 android“WebView
”中网站的 header。使用我拥有的代码,它可以工作。但问题是“WebView
”在页面完全加载后删除了 header。我想在加载时将其删除。
这就是代码片段:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('header')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('footer')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
}
});
使用 onPageStarted
方法代替 onPageFinished
。
来自 Android 文档 here
编辑: 阅读其他答案的评论,我看到您希望 header 再次显示。只需实施 onPageStarted
和 onPageEnded
,将 header 隐藏在一个中,然后在另一个中再次显示。
public class YourwebView extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl());
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header-full\"); header.parentNode.removeChild(header);");
}
}
还要确保您已启用 javascript
YourwebView myWebClient = new YourwebView();
webview.setWebViewClient(myWebClient);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
ANDROID WebView-Remove header 或 Android
中网页的任何标签
让我们了解一下这怎么可能,在我的例子中,我已经在 webview 中加载了 url 并且看起来像下面的屏幕截图,
Now from the above view if we want to remove the header and menu
portion of the Webpage. We need to use JSOUP lib with the help of
which you can remove the unnecessary portion of webpage.
步骤
在您的应用级构建中添加 jsoup 库 gradle
compile 'org.jsoup:jsoup:1.10.3'
现在,在 activity 中,我们将使用异步任务来解析/删除一些 html 导致网络操作无法直接在 UI 上完成的标签线。 Async Task 允许您执行后台操作并在 UI 线程上发布结果。常用的方法有3种。
onPreExecute():-Is used to display progress bar
doInBackground():-This step is used to perform background computation that can take a long time. You cannot update any UI in this method.
onPostExecute():-This is invoked after completion of background task and it will update the UI.
我已经在 xml 中定义了 WebView,如下所示,
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
并且在MainActivity.java中,我编写了JSOUP代码来删除网页中不需要的部分
public class MainActivity extends AppCompatActivity {
WebView webview;
String url="http://pixelay.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView) findViewById(R.id.webview);
new MyAsynTask().execute();
}
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
@Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document= Jsoup.connect(url).get();
document.getElementsByClass("main-navigation").remove();
document.getElementsByClass("custom-header-image").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}
});
}
}
}
现在,该看结果了,
最简单的方法是在 onLoadResource()
方法中注入 Javascript。将它放在 try-catch 块中,因为 WebView
在加载之前不知道该元素:
webView.setWebChromeClient(new WebChromeClient() {
...
@Override
public void onLoadResource(WebView view, String url) {
try {
webView.loadUrl("javascript:(window.onload = function() { " +
"(elem1 = document.getElementById('id1')); elem.parentNode.removeChild(elem1); " +
"(elem2 = document.getElementById('id2')); elem2.parentNode.removeChild(elem2); " +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
}
我想删除 android“WebView
”中网站的 header。使用我拥有的代码,它可以工作。但问题是“WebView
”在页面完全加载后删除了 header。我想在加载时将其删除。
这就是代码片段:
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('header')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
webView.loadUrl("javascript:(function() { " +
"var head = document.getElementsByTagName('footer')[0];"
+ "head.parentNode.removeChild(head);" +
"})()");
}
});
使用 onPageStarted
方法代替 onPageFinished
。
来自 Android 文档 here
编辑: 阅读其他答案的评论,我看到您希望 header 再次显示。只需实施 onPageStarted
和 onPageEnded
,将 header 隐藏在一个中,然后在另一个中再次显示。
public class YourwebView extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl());
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:var footer = document.getElementById(\"footer\"); footer.parentNode.removeChild(footer); var header = document.getElementById(\"header-full\"); header.parentNode.removeChild(header);");
}
}
还要确保您已启用 javascript
YourwebView myWebClient = new YourwebView();
webview.setWebViewClient(myWebClient);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
ANDROID WebView-Remove header 或 Android
中网页的任何标签让我们了解一下这怎么可能,在我的例子中,我已经在 webview 中加载了 url 并且看起来像下面的屏幕截图,
Now from the above view if we want to remove the header and menu portion of the Webpage. We need to use JSOUP lib with the help of which you can remove the unnecessary portion of webpage.
步骤
在您的应用级构建中添加 jsoup 库 gradle
compile 'org.jsoup:jsoup:1.10.3'
现在,在 activity 中,我们将使用异步任务来解析/删除一些 html 导致网络操作无法直接在 UI 上完成的标签线。 Async Task 允许您执行后台操作并在 UI 线程上发布结果。常用的方法有3种。
onPreExecute():-Is used to display progress bar
doInBackground():-This step is used to perform background computation that can take a long time. You cannot update any UI in this method.
onPostExecute():-This is invoked after completion of background task and it will update the UI.
我已经在 xml 中定义了 WebView,如下所示,
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
并且在MainActivity.java中,我编写了JSOUP代码来删除网页中不需要的部分
public class MainActivity extends AppCompatActivity {
WebView webview;
String url="http://pixelay.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView) findViewById(R.id.webview);
new MyAsynTask().execute();
}
private class MyAsynTask extends AsyncTask<Void, Void, Document> {
@Override
protected Document doInBackground(Void... voids) {
Document document = null;
try {
document= Jsoup.connect(url).get();
document.getElementsByClass("main-navigation").remove();
document.getElementsByClass("custom-header-image").remove();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
@Override
protected void onPostExecute(Document document) {
super.onPostExecute(document);
webview.loadDataWithBaseURL(url,document.toString(),"text/html","utf-8","");
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, request);
}
});
}
}
}
现在,该看结果了,
最简单的方法是在 onLoadResource()
方法中注入 Javascript。将它放在 try-catch 块中,因为 WebView
在加载之前不知道该元素:
webView.setWebChromeClient(new WebChromeClient() {
...
@Override
public void onLoadResource(WebView view, String url) {
try {
webView.loadUrl("javascript:(window.onload = function() { " +
"(elem1 = document.getElementById('id1')); elem.parentNode.removeChild(elem1); " +
"(elem2 = document.getElementById('id2')); elem2.parentNode.removeChild(elem2); " +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
}