【问题标题】:Why is window.navigator.storage.estimate() sometimes undefined on Chrome为什么 window.navigator.storage.estimate() 有时在 Chrome 上未定义
【发布时间】:2019-05-23 05:52:00
【问题描述】:

如果我从http://localhost 提供我的应用程序,我可以在开发控制台中使用window.navigator.storage.estimate()

如果我从http://example.com 提供相同的应用程序,其中example.com 从我的/etc/hosts 解析为127.0.0.1,则window.navigator 未定义。

怎么会?

(铬是 71)

【问题讨论】:

  • This feature is available only in secure contexts 但对于 localhost 来说可能没问题,因为它通常用于开发目的。
  • @Keith 你是对的。在挖掘时,我发现 Chrome 认为 localhost 是一个安全的上下文。随意发布这个作为答案。

标签: javascript google-chrome local-storage


【解决方案1】:

来自 MDN -> https://developer.mozilla.org/en-US/docs/Web/API/StorageEstimate/quota

看来此功能仅在安全上下文中可用,即 https://

但 Chrome 似乎认为 localhost 也是一个安全上下文,因此不需要 https://。我认为这是因为 localhost 通常用于开发目的,而为本地域获取 SSL 证书可能会很棘手。

更多信息在https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts 确认file://localhost 被认为是安全的。 if (window.isSecureContext) 可用于测试状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2015-05-25
    • 2018-07-21
    • 1970-01-01
    • 2020-11-23
    • 2012-10-03
    相关资源
    最近更新 更多