Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android Webview, am I able to modify a webpage's DOM?

Suppose I load a 3rd party URL through webview.

public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         webview = (WebView) findViewById(R.id.webview);         webview.setWebViewClient(new HelloWebViewClient());         webview.getSettings().setJavaScriptEnabled(true);         webview.setWebChromeClient(new MyWebChromeClient());         webview.loadUrl("http://ebay.com");              } 

Is it possible for me to inject something into this WebView to replace the ebay logo with my own?

like image 969
TIMEX Avatar asked Feb 08 '10 01:02

TIMEX


People also ask

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

What is a WebView And how do you add one to your layout?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.


2 Answers

To expand on CommonsWare's correct answer:

WebView webview = new WebView(); webview.setWebViewClient(new WebClient()); webView.getSettings().setJavaScriptEnabled(true); webview.loadUrl("stackoverflow.com"); 

then in WebClient:

public class WebClient extends WebViewClient {     @Override     public boolean shouldOverrideUrlLoading(WebView view, String url) {         view.loadUrl(url);         return true;     }      @Override     public void onPageFinished(WebView view, String url)      {                // Obvious next step is: document.forms[0].submit()         view.loadUrl("javascript:document.forms[0].q.value='[android]'");            } } 

In a nutshell, you wait for the page to load. Then you loadUrl("javascript:[your javascript here]").

like image 129
Carlos Rendon Avatar answered Oct 02 '22 00:10

Carlos Rendon


Not directly. You can invoke Javascript code in the context of the current Web page, via loadUrl(), much like a bookmarklet does. However, you do not have direct access to the DOM from Java code.

like image 24
CommonsWare Avatar answered Oct 02 '22 00:10

CommonsWare