In Android Webview, am I Able to Modify a Webpage's Dom

WebView: Manipulate DOM after webpage has been loaded

I guess this is very close to your needs:

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

So it is possible using loadUrl("javascript:[some javascript here]") after onPageFinished().

Android WebView: display only some part of website

You can do this by extending WebViewClient and injecting some javascript which will render your web Page

public class MyWebClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:your javascript");
}
}
.........
final MyWebClient myWebViewClient = new MyWebClient();
mWebView.setWebViewClient(myWebViewClient);

For hiding elements use view.loadUrl("javascript:document.getElementById(id).style.display = 'none';)

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

How to save an Android webview to DOM, and load a webview from a given DOM?

As I have mentioned in comments, it is not possible to access contents of local storage from another application. Instead you can save the contents to a file and use that in the other application.

Get DOM:

from webview,

var dom = document.getElementsByTagName('html')[0].outerHTML;

Store it to file using Javascript Interface

From documentation;

Injects the supplied Java object into this WebView. The object is
injected into the JavaScript context of the main frame, using the
supplied name. This allows the Java object's methods to be accessed
from JavaScript. For applications targeted to API level JELLY_BEAN_MR1
and above, only public methods that are annotated with
JavascriptInterface can be accessed from JavaScript.

Class FileWriter{

@JavascriptInterface
public void writeToFile(String dataToBeWritten) {
writeDataToFile(dataToBeWritten);
}

public static void writeDataToFile(Sting data){
// java code to write data to file
}
}

In your main activity,

webView.addJavascriptInterface(new FileWriter(), "FileWriter");

This exposes FileWriter as an object in global namespace in JavaScript land of webview.

Now from JavaScript:

FileWriter.writeToFile(dom);

Will create a file in the specified location with passed data.

Hope this helps.

How can I prevent an Android WebView from manipulating a web page?

It is not really possible to prevent the web response being modified. You can only think of making it difficult for the spoofer to edit it.



Related Topics



Leave a reply



Submit