Untitled

From Sweet Finch, 12 Years ago, written in Plain Text, viewed 4 times.
URL https://paste.blessuren.de/view/e67cc6f3 Embed
Download Paste or View Raw
  1. package com.example.webviewdemo;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.view.MotionEvent;
  8. import android.view.View;
  9. import android.webkit.WebView;
  10. import android.webkit.WebViewClient;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         final WebView webview = (WebView) findViewById(R.id.wv);
  20.         webview.setVisibility(View.INVISIBLE);
  21.        
  22.         webview.getSettings().setLoadsImagesAutomatically(false);
  23.         webview.getSettings().setJavaScriptEnabled(true);
  24.         webview.setClickable(false);
  25.         webview.setHorizontalScrollBarEnabled(false);
  26.        
  27.         webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
  28.        
  29.          
  30.        
  31.        
  32.        
  33.         webview.setWebViewClient(new WebViewClient() {
  34.             @Override
  35.             public void onPageFinished(WebView view, String url) {
  36.                 webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
  37.                         "('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
  38.             }
  39.         });
  40.  
  41.         webview.loadUrl("http://4fuckr.com/image_4246365.htm");
  42.    
  43.    
  44. }
  45.    
  46.     class MyJavaScriptInterface {
  47.  
  48.         private Context ctx;
  49.  
  50.         MyJavaScriptInterface(Context ctx) {
  51.             this.ctx = ctx;
  52.         }
  53.  
  54.         public void showHTML(String html) {
  55.             /*new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
  56.                     .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();*/
  57.                 getComments(html);
  58.         }
  59.        
  60.     }
  61.  
  62.    
  63.         public int getComments(String html){
  64.                
  65.                 final WebView webview = (WebView) findViewById(R.id.wv);
  66.                 int pos1=0;
  67.                 int pos2=0;
  68.                 String comments="";
  69.                 String comments_adfree="";
  70.                
  71.                 if (html.isEmpty()){
  72.                         return -1;
  73.                 }
  74.                 pos1=html.indexOf("<div id=\"comment-show\">");
  75.                 pos2=html.indexOf("<script type=\"text/javascript\">",pos1);
  76.                
  77.                 if(pos1==-1||pos2==-1||pos1==0||pos2==0){
  78.                         Toast msg = Toast.makeText(getBaseContext(),
  79.                                         "Error - can´t load/find comments",
  80.                                         Toast.LENGTH_SHORT);
  81.                         msg.show();
  82.                         return -1;
  83.                 }
  84.                                
  85.        
  86.             comments=html.substring(pos1,pos2);
  87.                
  88.                 // Remove ADDs
  89.                 pos1=comments.indexOf("<iframe");
  90.                         if(pos1>=0){
  91.                                 //Werbung gefunden
  92.                                 pos2=comments.indexOf("</iframe>");
  93.                                 comments_adfree=comments.substring(0,pos1);
  94.                                 comments_adfree=comments_adfree.concat(comments.substring(pos2,comments.length()));
  95.                         }
  96.                
  97.                
  98.             webview.setClickable(false);
  99.             webview.getSettings().setLoadsImagesAutomatically(false);
  100.             webview.setHorizontalScrollBarEnabled(false);
  101.                 webview.clearView();
  102.                 webview.loadData(comments_adfree, "text/html", "UTF-8");
  103.    
  104.                 webview.setVisibility(View.VISIBLE);
  105.    
  106.                
  107.                
  108.                
  109.                 return 0;
  110.                
  111.    
  112.                
  113.                
  114.                
  115.         }
  116.  
  117.    
  118. }
  119.    
  120.  
  121.  

Reply to "Untitled"

Here you can reply to the paste above