Android - How to access local storage values in WebView?

18,934

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.

Share:
18,934
Mark
Author by

Mark

Updated on June 27, 2022

Comments

  • Mark
    Mark almost 2 years

    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.