Untitled

From Sludgy Owl, 12 Years ago, written in Plain Text, viewed 3 times.
URL https://paste.blessuren.de/view/772e84e5 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.View;
  8. import android.webkit.WebView;
  9. import android.webkit.WebViewClient;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends Activity {
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.         final WebView webview = (WebView) findViewById(R.id.wv);
  19.         webview.setVisibility(View.INVISIBLE);
  20.         webview.getSettings().setJavaScriptEnabled(true);
  21.         webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");
  22.  
  23.         webview.setWebViewClient(new WebViewClient() {
  24.             @Override
  25.             public void onPageFinished(WebView view, String url) {
  26.                 webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
  27.                         "('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
  28.             }
  29.         });
  30.  
  31.         webview.loadUrl("http://4fuckr.com/image_4246365.htm");
  32.    
  33.    
  34. }
  35.    
  36.     class MyJavaScriptInterface {
  37.  
  38.         private Context ctx;
  39.  
  40.         MyJavaScriptInterface(Context ctx) {
  41.             this.ctx = ctx;
  42.         }
  43.  
  44.         public void showHTML(String html) {
  45.             /*new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
  46.                     .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();*/
  47.                 getComments(html);
  48.         }
  49.        
  50.     }
  51.  
  52.    
  53.         public int getComments(String html){
  54.                
  55.                 final WebView webview = (WebView) findViewById(R.id.wv);
  56.                 int pos1=0;
  57.                 int pos2=0;
  58.                 String comments="";
  59.                
  60.                
  61.                 pos1=html.indexOf("<div id=\"comment-show\">");
  62.                 pos2=html.indexOf("<script type=\"text/javascript\">",pos1);
  63.                
  64.                 if(pos1==-1||pos2==-1||pos1==0||pos2==0){
  65.                         Toast msg = Toast.makeText(getBaseContext(),
  66.                                         "Error - can´t load/find comments",
  67.                                         Toast.LENGTH_SHORT);
  68.                         msg.show();
  69.                         return -1;
  70.                 }
  71.                
  72.                 comments=html.substring(pos1,pos2);
  73.                 webview.clearView();
  74.                 webview.loadData(comments, "text/html", "UTF-8");
  75.                
  76.                
  77.                 return 0;
  78.                
  79.                
  80.                
  81.                
  82.         }
  83.  
  84.    
  85. }
  86.    
  87.  
  88.  

Reply to "Untitled"

Here you can reply to the paste above