window.localStorage与chrome.storage.local

卡伦·基什米尔扬:

我正在开发Chrome扩展程序,需要存储一些数据,然后在某个时候获取它。我对available 进行了调查,storage并发现了以下内容:window.localStoragechrome.storage.local

所以我的问题是,在Chrome扩展程序中使用哪个是正确的选择:
window.localStorage还是chrome.storage.local

PS我使用browser action加载本地HTMLIFRAME所以我没有用popup.js

jmort253:

这完全取决于您的Chrome扩展程序将要执行的操作。window.localStorage是HTML5存储。除非您在后台页面中运行它,否则它只能允许您获取数据并将其设置到特定域的存储中。对于注入到DOM中的代码也是如此,因为它将使用网页上的localStorage。

换句话说,除非您在后台页面中使用localStorage,否则它将无法在不同的Web页面之间共享数据,因为该页面以chrome:// URI作为其域,因此它独立于网页运行。

而chrome.storage.local专为Chrome扩展程序和Chrome应用程序设计,用于将数据存储在更中央的位置。由于普通网页无法访问此扩展,因此每个扩展都拥有自己的存储空间。一种可能性是背景页面处理数据的设置和获取,而内容脚本处理网页的修改和交互。

但是,这些API也在内容脚本中起作用,并且我编写的两个扩展都使用从内容脚本中调用的chrome.storage.local。

作为示例,我构建了一个Stack App,该应用程序将Stack Exchange中的收件箱项保留到您实际阅读它们之前,称为StackInbox由于Stack Exchange网站跨越数百个域,因此我选择了chrome.storage.local,因为我可以保存用户的accountId并在所有站点之间重用它,以确保收件箱数据同步,同时也可以直接在内容脚本中使用它。

作为一个简单的测试,将一些数据放在内容脚本的一个域中的localStorage中,然后尝试将其从另一个域中拉出,您将看到数据将不存在。使用chrome.storage.local,这不是问题。

最后,由于用户选择安装Chrome扩展程序和Chrome应用,因此将其列入了白名单,因此它们通常可以比普通网站执行更多操作。例如,通过在清单中指定“ unlimitedStorage”许可权,您可以存储远远超出HTML5 localStorage设置的5MB限制的数据。

有关更多信息,请参阅Chrome存储上的Google文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

开发Chrome扩展程序时如何清除“ chrome.storage.local”缓存?

chrome.storage.local.get和set

是否可以在开发人员工具中查看chrome.storage.local?

Chrome应用程式localStorage无法持续运作,而chrome.storage无法运作

使用chrome.storage.local.set存储数据的安全性如何

chrome.storage.local。在chrome.runtime.reload()之后获取空-打包的应用

从JavaScript中的chrome.storage.local中删除以字符串开头的变量

用玩笑测试chrome.storage.local.set

Prometheus storage.tsdb.retention与storage.local.retention?

localStorage:我无法使用@ ngx-pwa / local-storage删除项目

为什么有时在Chrome上未定义window.navigator.storage.estimate()

chrome.storage.local get返回“未定义”

无法使用chrome.storage.local.set保存集

在包含chrome.storage.local中保存的对象数组的对象中检查密钥

覆盖Chrome Storage API

Chrome.storage.local.get在内容脚本中不起作用

从localStorage迁移到chrome.storage

Chrome.Storage.Local持久性

从chrome.storage.local获取原始值

如何使用chrome.storage.local.get和XMLHttpRequest同步执行功能?

为什么chrome.storage.local仅保存最后一条记录?

chrome扩展名:从本地存储切换到chrome.storage.local

如何否定 chrome.storage.local 中的变量

如何删除 chrome.storage.local

chrome.storage.local 写入密钥的问题

chrome.storage.local.get 在浏览器启动期间返回 undefined,但仅针对特定值

使用 chrome.storage.local.set 时 JSON 对象损坏

为什么 chrome.storage.local.get 返回 undefined 的值?

Chrome 擴展開發:chrome.storage.local vs Indexeddb