Injecting javascript in android webview
Its easy, make sure you have javascript enabled and simply load url containing javascript:mBrowser = new WebView(context); mBrowser.getSettings().setJavaScriptEnabled(true); mBrowser.loadUrl("javascript:getElementById('ad_container').style.display='none');");
In most cases, you first want to load the actual web page, then inject the javascript - this is done by using a WebViewClient which overrides onPageFinished.
mBrowser.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { // check url if (SOME_WEBPAGE.equals(url) { loadUrl(JAVASCRIPT_CODE); } } }); mBrowser.loadUrl(SOME_WEBPAGE);
Extracting HTML from Android webview
Taking it one step further - let's get HTML from a page and use it in our program. This is accomplished by using a JavascriptInterface with our webview.mBrowser.getSettings().setJavaScriptEnabled(); // add javascript interface with example method mBrowser.addJavaScriptInterface(new JavaScriptInterface() { public void setTitle(String html) { mTitle = html; } }, "MY_JS"); // add webview client that calls javascript interface when page is loaded mBrowser.addWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { view.loadUrl("javascript:window.MY_JS.setTitle(document.getElementsByTagName('title')[0].innerHTML);"); } mBrowser.loadUrl(SOME_WEBPAGE);
That's it - using the combination of WebVievClient and JavascriptInterface is very powerful - drawback is that I have not found an easy way of debugging javascript - if your javascript fails it will fail silent.
Nice thing is you can test your javascript on Firefox using Firebug on your main computer before adding code to your android project.
If you just want to download the HTML from an URL it feels like there are easier ways to do that, but that isn't your main goal I guess?
ReplyDeleteOtherwise I usually use HttpGet but then you can't navigate and then just get the HTML...
If you don't need/want all the features from webview but just want the http response you may want to consider using AndroidHttpClient (API 8 [2.2-Froyo]) or DefaultHttpClient (API 1).
ReplyDeleteTrue, you do have some extra features in the webview. I rest my case :)
ReplyDeleteDon't rest it just yet =) It depends on what you need. A lightweight HttpClient will be a much faster option if you just want to parse some html contents. The webview will load css, javascript (unless 2.2 - blockNetworkLoad) etc. increasing network load. But in my case, for TravAlert, having the webview manage authentication and session cookies makes it all worth it.
ReplyDeleteThe guys above does not undsrstand one thing it is damn easy to fetch content of some remote address. The guide here is essential when you use WebView in your application and want to debug the received HTML page, when at the same time the request should be made via VewbView and not with some AndroidHttpClient, as we need exact browser headers and proxies to be used.
ReplyDelete[quote]drawback is that I have not found an easy way of debugging javascript[/quote]
ReplyDeleteAdd a WebChromeClient to your WebView and override the WebChromeClient's onConsoleMessage method.
You can re-direct all WebView console output to the android logcat.
Easy example here:
http://developer.android.com/guide/webapps/debugging.html
warwound
Yes, but still - console output is a very limited and time consuming way of debugging.
ReplyDeletewhile paste the above code in my program,there are so many errors,so please give this code as a project or give entire code.
ReplyDeleteplease,its urgent
can you give a working example
ReplyDeleteI know the above concept work fine.But i cannot realize how to implement it.Beginners cannot follow the above.So can you please give a working example.
Thank You Very Much
KIRAN
This comment has been removed by a blog administrator.
ReplyDeleteThanks man, just what I was looking for! :)
ReplyDeleteThis comment has been removed by the author.
Deletecould you please give a working example.i am a beginner.i tried many examples.
Deletewanted to. Thanks either way.
ReplyDeleteLet me justify that in perfect detail.
negative ion generator reviews consumer reports
air purifier made in germany
meat purifier