- package com.example.webviewdemo;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final WebView webview = (WebView) findViewById(R.id.wv);
- webview.setVisibility(View.INVISIBLE);
- webview.getSettings().setLoadsImagesAutomatically(false);
- webview.getSettings().setJavaScriptEnabled(true);
- webview.setClickable(false);
- webview.setHorizontalScrollBarEnabled(false);
- webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
- webview.setWebViewClient(new WebViewClient() {
- @Override
- public void onPageFinished(WebView view, String url) {
- webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
- "('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
- }
- });
- webview.loadUrl("http://4fuckr.com/image_4246365.htm");
- }
- class MyJavaScriptInterface {
- private Context ctx;
- MyJavaScriptInterface(Context ctx) {
- this.ctx = ctx;
- }
- public void showHTML(String html) {
- /*new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
- .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();*/
- getComments(html);
- }
- }
- public int getComments(String html){
- final WebView webview = (WebView) findViewById(R.id.wv);
- int pos1=0;
- int pos2=0;
- String comments="";
- String comments_adfree="";
- if (html.isEmpty()){
- return -1;
- }
- pos1=html.indexOf("<div id=\"comment-show\">");
- pos2=html.indexOf("<script type=\"text/javascript\">",pos1);
- if(pos1==-1||pos2==-1||pos1==0||pos2==0){
- Toast msg = Toast.makeText(getBaseContext(),
- "Error - can´t load/find comments",
- Toast.LENGTH_SHORT);
- msg.show();
- return -1;
- }
- comments=html.substring(pos1,pos2);
- // Remove ADDs
- pos1=comments.indexOf("<iframe");
- if(pos1>=0){
- //Werbung gefunden
- pos2=comments.indexOf("</iframe>");
- comments_adfree=comments.substring(0,pos1);
- comments_adfree=comments_adfree.concat(comments.substring(pos2,comments.length()));
- }
- webview.setClickable(false);
- webview.getSettings().setLoadsImagesAutomatically(false);
- webview.setHorizontalScrollBarEnabled(false);
- webview.clearView();
- webview.loadData(comments_adfree, "text/html", "UTF-8");
- webview.setVisibility(View.VISIBLE);
- return 0;
- }
- }