<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lexanderA &#187; WebViewClient</title>
	<atom:link href="http://lexandera.com/tag/webviewclient/feed/" rel="self" type="application/rss+xml" />
	<link>http://lexandera.com</link>
	<description>A blog about the web, mobile web, semantic web and mobile semantic web.</description>
	<lastBuildDate>Sun, 06 Jun 2010 18:27:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Intercepting page loads in WebView</title>
		<link>http://lexandera.com/2009/02/intercepting-page-loads-in-webview/</link>
		<comments>http://lexandera.com/2009/02/intercepting-page-loads-in-webview/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 05:20:38 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[WebView examples]]></category>
		<category><![CDATA[WebView]]></category>
		<category><![CDATA[WebViewClient]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=213</guid>
		<description><![CDATA[It is time for another WebView example. This time on how you can intercept any attempts at loading a web page and redirect the user to a different URL instead.
This can be achieved simply by registering a WebViewClient which overrides the shouldOverrideUrlLoading() method in which you tell the WebView to load a different URL from [...]]]></description>
			<content:encoded><![CDATA[<p>It is time for another <a href="http://code.google.com/android/reference/android/webkit/WebView.html">WebView</a> example. This time on how you can intercept any attempts at loading a web page and redirect the user to a different URL instead.</p>
<p>This can be achieved simply by registering a <a href="http://code.google.com/android/reference/android/webkit/WebViewClient.html">WebViewClient</a> which overrides the <a href="http://code.google.com/android/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String)">shouldOverrideUrlLoading</a>() method in which you tell the WebView to load a different URL from the one the user had requested.</p>
<p>The example below loads lexandera.com first, but when the user clicks any link on the page, he is redirected to yahoo.com:</p>
<pre name="code" class="java">

WebView browser = (WebView)findViewById(R.id.browser);
browser.setWebViewClient(new WebViewClient() {
    /* On Android 1.1 shouldOverrideUrlLoading() will be called every time the user clicks a link,
     * but on Android 1.5 it will be called for every page load, even if it was caused by calling loadUrl()! */
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        /* intercept all page load attempts and load yahoo.com instead */
        String myAlternativeURL = &quot;http://yahoo.com&quot;;
        if (!url.equals(myAlternativeURL)) {
            view.loadUrl(myAlternativeURL);
            return true;
        }

        return false;
    }
});

browser.loadUrl(&quot;http://lexandera.com/&quot;);
</pre>
<p><del datetime="2009-08-25T05:42:59+00:00">Please note that calling <a href="http://code.google.com/android/reference/android/webkit/WebView.html#loadUrl(java.lang.String)">loadUrl</a>() does not trigger the shouldOverrideUrlLoading() method, which means that we do not need to worry about endless recursion when calling loadUrl() from within shouldOverrideUrlLoading()! </del></p>
<p><strong>UPDATE</strong>: The above statement is no longer true for Android 1.5! Calling <a href="http://code.google.com/android/reference/android/webkit/WebView.html#loadUrl(java.lang.String)">loadUrl</a>() will now also trigger the shouldOverrideUrlLoading() method! This means that you need to make sure that you are not creating an infinite loop when calling loadUrl() from shouldOverrideUrlLoading()!</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/ed91eca5-2c5e-4b86-8b88-8af5a1b703f7/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=ed91eca5-2c5e-4b86-8b88-8af5a1b703f7" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/02/intercepting-page-loads-in-webview/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Extracting HTML from a WebView</title>
		<link>http://lexandera.com/2009/01/extracting-html-from-a-webview/</link>
		<comments>http://lexandera.com/2009/01/extracting-html-from-a-webview/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 19:30:33 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[WebView examples]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebView]]></category>
		<category><![CDATA[WebViewClient]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=200</guid>
		<description><![CDATA[Here&#8217;s another Android WebView tutorial for those of you who are looking for a way to get the source code of a page loaded in a WebView instance.
This example is a bit more complicated than previous ones, so let me explain it step by step:

First, a class called MyJavaScriptInterface is defined. It implements a single [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s another Android <a href="http://code.google.com/android/reference/android/webkit/WebView.html">WebView</a> tutorial for those of you who are looking for a way to get the source code of a page loaded in a WebView instance.</p>
<p>This example is a bit more complicated than previous ones, so let me explain it step by step:</p>
<ul>
<li>First, a class called MyJavaScriptInterface is defined. It implements a single public method showHTML() which displays a dialog with the HTML it receives as a parameter.</li>
<li>Then, an instance of this class is registered as a JavaScript interface called HTMLOUT. The showHTML() method can now be accessed from JavaScript like this: window.HTMLOUT.showHTML(&#8216;&#8230;&#8217;) </li>
<li>In order to call showHTML() when the page finishes loading, a <a href="http://code.google.com/android/reference/android/webkit/WebViewClient.html">WebViewClient</a> instance which overrides <a href="http://code.google.com/android/reference/android/webkit/WebViewClient.html#onPageFinished%28android.webkit.WebView,%20java.lang.String%29">onPageFinished</a>() is added to the WebView. When the page finises loading, this method will inject a piece of JavaScript code into the page, using <a href="http://lexandera.com/2009/01/injecting-javascript-into-a-webview/">the method I described in an earlier post</a>.</li>
<li>Finally, a web page is loaded.</li>
</ul>
<pre name="code" class="java">

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
    @SuppressWarnings(&quot;unused&quot;)
    public void showHTML(String html)
    {
        new AlertDialog.Builder(myApp)
            .setTitle(&quot;HTML&quot;)
            .setMessage(html)
            .setPositiveButton(android.R.string.ok, null)
        .setCancelable(false)
        .create()
        .show();
    }
}

final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), &quot;HTMLOUT&quot;);

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url)
    {
        /* This call inject JavaScript into the page which just finished loading. */
        browser.loadUrl(&quot;javascript:window.HTMLOUT.showHTML(&#039;&lt;head&gt;&#039;+document.getElementsByTagName(&#039;html&#039;)[0].innerHTML+&#039;&lt;/head&gt;&#039;);&quot;);
    }
});

/* load a web page */
browser.loadUrl(&quot;http://lexandera.com/files/jsexamples/gethtml.html&quot;);
</pre>
<p><strong>WARNING</strong><br />
Unfortunately, this approach suffers from a major security hole: if your JavaScript can call showHTML(), then so can JavaScript from every other page that might get loaded into the WebView. Use with care.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/bb22593d-ec9d-4064-9842-2bcb7fae4d4f/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=bb22593d-ec9d-4064-9842-2bcb7fae4d4f" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/01/extracting-html-from-a-webview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Injecting JavaScript into a WebView</title>
		<link>http://lexandera.com/2009/01/injecting-javascript-into-a-webview/</link>
		<comments>http://lexandera.com/2009/01/injecting-javascript-into-a-webview/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 06:05:21 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebView examples]]></category>
		<category><![CDATA[WebChromeClient]]></category>
		<category><![CDATA[WebView]]></category>
		<category><![CDATA[WebViewClient]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=64</guid>
		<description><![CDATA[Back in early &#8216;08, while the Androd developer challenge was in full swing, a fairly common question which went without a satisfactory answer was: &#8220;How can I manipulate the contents of a WebView?&#8221;. I now finally have the answer, thanks to a couple of lines of code I stumbled upon while looking at the source [...]]]></description>
			<content:encoded><![CDATA[<p>Back in early &#8216;08, while the <a href="http://code.google.com/android/adc.html">Androd developer challenge</a> was in full swing, a fairly common question which went without a satisfactory answer was: &#8220;How can I manipulate the contents of a WebView?&#8221;. I now finally have the answer, thanks to a couple of lines of code I stumbled upon while looking at the source of <a href="http://www.jsharkey.org/" title="Jeffrey Sharkey" rel="homepage" class="zem_slink">Jeffrey Sharkey</a>&#8217;s <a href="http://www.jsharkey.org/blog/2008/12/15/oilcan-greasemonkey-on-steroids-for-android/">OilCan</a>.</p>
<p>The solution is actually very simple: wait for the original page to finish loading, then inject your own JavaScript code into it by calling webview.loadUrl(&#8220;javascript:your-code-here&#8221;) when your webvew&#8217;s onPageFinished() event is triggered. Loading an URL beginning with &#8220;javascript:&#8221; is the exact same approach used by <a href="http://en.wikipedia.org/wiki/Bookmarklet" title="Bookmarklet" rel="wikipedia" class="zem_slink">bookmarklets</a>.</p>
<p>Here&#8217;s a simple example which loads a page and then sets its text color to red:</p>
<pre name="code" class="java">

final WebView webview = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
webview.getSettings().setJavaScriptEnabled(true);

/* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url)
    {
        webview.loadUrl(&quot;javascript:(function() { &quot; +
                &quot;document.getElementsByTagName(&#039;body&#039;)[0].style.color = &#039;red&#039;; &quot; +
                &quot;})()&quot;);
    }
});

webview.loadUrl(&quot;http://code.google.com/android&quot;);
</pre>
<p>That&#8217;s it.</p>
<p>Please note though that <em>alert()</em> and <em>confirm()</em> JavaScript functions will not work unless you also register a <a href="http://code.google.com/android/reference/android/webkit/WebChromeClient.html">WebChromeClient</a> which implements the required methods.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/75a6582e-c459-4c4e-848b-e48a050ab4c9/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=75a6582e-c459-4c4e-848b-e48a050ab4c9" alt="Reblog this post [with Zemanta]"></a></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/01/injecting-javascript-into-a-webview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
