Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to intercept javascript triggered URL in webview?

Is there any way to intercept javascript triggered URL in a WebView just like normal hrefs using shouldOverrideUrlLoading()?

like image 606
bhups Avatar asked Aug 26 '10 06:08

bhups


People also ask

How do you intercept a URL in WebView?

setWebViewClient(new WebViewClient() { @Override public void onLoadResource(WebView view, String url) { if (url. startsWith("app://")) { Intent i = new Intent(Intent. ACTION_VIEW, Uri. parse(url), getContext(), Main.

Can WebView run JavaScript?

WebView is a special component in Android which serves as kind of built-in browser inside Android applications. If you want to execute HTML, CSS or JavaScript code in your Android app, or you need to allow users visit a URL without leaving your application, WebView is the way to go.

What is shouldOverrideUrlLoading?

shouldOverrideUrlLoading is called when a new page is about to be opened whereas shouldInterceptRequest is called each time a resource is loaded like a css file, a js file etc.

What is WebViewClient in Android?

Android System WebView is a system component for the Android operating system (OS) that enables Android apps to display web content directly inside an application.


1 Answers

onLoadResource() is called when any resource is being called, including JavaScript. The purpose is a bit different though than shouldOverrideUrlLoading().

webControls.setWebViewClient(new WebViewClient() { 
    //Notify the host application that the WebView will load 
    //the resource specified by the given url.
    @Override  
    public void onLoadResource (WebView view, String url) {
        super.onLoadResource(view, url);
        Log.v(TAG, "onLoadResource("+url+");");
    }

    //Give the host application a chance to take over the 
    //control when a new url is about to be loaded in the 
    //current WebView. 
    @Override  
    public void shouldOverrideUrlLoading(WebView view, String url) {
        super.shouldOverrideUrlLoading(view, url);
        Log.v(TAG, "shouldOverrideUrlLoading("+url+");");
    }
}
like image 87
JasonCG Avatar answered Sep 27 '22 15:09

JasonCG