Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to access local storage values in WebView?

Is it possible to access the Local Storage data from a WebView? Here is a screenshot of Chrome of the values that I am needing to read from my app's WebView. I am needing to get the data from the "Value" column in my app.

What I am trying to do, for example, is go to google.com in my WebView, then read the value "1" from the key "nullctx".

Chrome local storage

Any help of how to read these values would be appreciated.

like image 234
Mark Avatar asked May 19 '16 14:05

Mark


People also ask

Do cookies work in WebView?

Cookies storing is an essential part of Android development, used extensively in authentication. Here I'm sharing… The focus here is fully enabling the Cookies to be shared between the Native and the Webview.

How do I access local storage on Android?

You can also access free internal storage from your Android phone through Settings > System > Storage > Device storage. Here you can preview what data are using your internal storage and how much free storage you can use furtherly.


1 Answers

window.localStorage returns all the values in local storage.

You can print them all to console by `console.log(window.localStorage);

You can use Console APIs in WebView to get the stuff from console

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient() {
  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    Log.d("MyApplication", message + " -- From line "
                         + lineNumber + " of "
                         + sourceID);
  }
});

You can try with following HTML if you want `

<html>
<body>
value read from localstorage:<input type="text" id="test"><br>
value read from localstorage:<input type="text" id="test2"><p>
Open Console to read all values
<script>
window.localStorage.setItem("name", "Peter");
window.localStorage.setItem("name2", "Tom");
window.localStorage.setItem("name3", "Steve");
window.localStorage.setItem("name4", "Ian");
test.value=window.localStorage["name"];
test2.value=window.localStorage["name3"];
console.log(window.localStorage);
</script>
</body>
</html>

But this is for things that are stored in your page. You cannot directly read information from other pages in iFrame from different domain. However, if you own all those pages, you can do use info from Cross-Domain LocalStorage.

like image 61
ankit9j Avatar answered Oct 29 '22 01:10

ankit9j