WebView と WebViewClient を使って HTML のソースを LogCat に流す

public class HogeActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final WebView webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new HogeWebViewClinet());
        webView.addJavascriptInterface(new WebViewLogger(), "webViewLogger");
        webView.loadUrl("http://www.yahoo.co.jp/");
        setContentView(webView);
    }
    
    class HogeWebViewClinet extends WebViewClient {
        @Override public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:window.webViewLogger.log(document.documentElement.outerHTML);");
        }
    }
    
    class WebViewLogger {
        public void log(String str) {
            Log.d("webViewLogger", str);
        }
    }
    
}

1 件のコメント: