6

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.

Mark
  • 1,130
  • 3
  • 17
  • 32
  • Your question is not clear to me: are you trying to access your local storage from the web view using Javascript? Or you want to save something into your local storage from the web view and then read it from Java code? – racs May 29 '16 at 21:25
  • I am just trying to read the values, If javascript is the only way to do it, then that is what I need to know. There is a webpage that is storing values in the Local Storage and I can see them fine on a desktop using Chrome. I just need to read these values in my app from a WebView. – Mark May 31 '16 at 03:56
  • You can read back whatever you stored there, otherwise it would be a major security problem obviously. First of all here is how to work with local store if you were not familiar with the concept: http://www.w3schools.com/html/html5_webstorage.asp Also you need to enable web storage on your web view before you could store anything: http://stackoverflow.com/questions/5899087/android-webview-localstorage I hope this helps, maybe this is not what you are looking for. – racs May 31 '16 at 04:33
  • 1
    You may check Stetho >> http://facebook.github.io/stetho/ – mgcaguioa Jun 02 '16 at 08:09

1 Answers1

2

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.

ankit9j
  • 254
  • 2
  • 7
  • Unfortunately, I don't have access to the webpage. What I am trying to do, for example, is go to google.com in my WebView, then from the Local Storage, read the value "1" from the key "nullctx" (example from the screenshot above). – Mark Jun 01 '16 at 17:06
  • You can read it by `window.localStorage["nullctx"]` if google.com is the main page open in webview and its not open in an iframe. You can inject code for console.log in google.com by following http://stackoverflow.com/questions/21552912/android-web-view-inject-local-javascript-file-to-remote-webpage If you can get the value in Chrome inspector, you can get it in webview. You can try `window.localStorage` in your Google Chrome console by opening google.com – ankit9j Jun 03 '16 at 03:33
  • very smart, i like it :) – Ali insan Soyaslan Feb 01 '19 at 19:39