Android-如何在WebView中访问本地存储值? [英] Android - How to access local storage values in WebView?

查看:302
本文介绍了Android-如何在WebView中访问本地存储值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从WebView访问本地存储数据?这是我需要从应用程序的WebView中读取值的Chrome的屏幕截图.我需要从应用程序的值"列中获取数据.

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.

例如,我要执行的操作是在WebView中转到google.com,然后从键"nullctx"中读取值"1".

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".

任何有关如何读取这些值的帮助将不胜感激.

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

推荐答案

window.localStorage返回本地存储中的所有值.

window.localStorage returns all the values in local storage.

您可以通过`console.log(window.localStorage);将它们全部打印到控制台上

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

您可以使用 WebView中的控制台API 从中获取内容控制台

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);
  }
});

如果需要,您可以尝试使用以下HTML `

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>

但这是针对您页面中存储的内容的.您无法直接从其他域读取iFrame中其他页面的信息.但是,如果您拥有所有这些页面,则可以使用交叉-域LocalStorage .

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.

这篇关于Android-如何在WebView中访问本地存储值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆