有没有一种方法可以增加Google Chrome中localStorage的大小,以避免QUOTA_EXCEDED_ERR:DOM异常22

kg7wmglp  于 2023-06-19  发布在  Go
关注(0)|答案(7)|浏览(216)

我写了一个web应用程序,允许您将图像存储在localStorage中,直到您点击保存(因此,如果信号不好,它可以离线工作)。
当localStorage达到5MB时,Google Chrome会在JavaScript控制台日志中生成错误:
未捕获错误:QUOTA_EXCEEDED_ERR:DOM异常22
如何在Google Chrome上增加localStorage配额的大小?

r6vfmomb

r6vfmomb1#

5 MB是一个硬限制,这是愚蠢的。IndexedDB给你~ 50 MB,这是更合理的。为了更容易使用,请尝试Dexie.js https://github.com/dfahlander/Dexie.js

更新

实际上,Dexie.js对于我简单的键-值目的来说仍然是大材小用,所以我编写了这个简单得多的脚本https://github.com/DVLP/localStorageDB
这样你就有了50 MB的空间,可以获取和设置类似的值

// Setting values
ldb.set('nameGoesHere', 'value goes here');

// Getting values - callback is required because the data is being retrieved asynchronously:
ldb.get('nameGoesHere', function (value) {
  console.log('And the value is', value);
});

复制/粘贴下面的行,以便上面示例中的ldb.set()ldb.get()可用。

!function(){function e(t,o){return n?void(n.transaction("s").objectStore("s").get(t).onsuccess=function(e){var t=e.target.result&&e.target.result.v||null;o(t)}):void setTimeout(function(){e(t,o)},100)}var t=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB;if(!t)return void console.error("indexDB not supported");var n,o={k:"",v:""},r=t.open("d2",1);r.onsuccess=function(e){n=this.result},r.onerror=function(e){console.error("indexedDB request error"),console.log(e)},r.onupgradeneeded=function(e){n=null;var t=e.target.result.createObjectStore("s",{keyPath:"k"});t.transaction.oncomplete=function(e){n=e.target.db}},window.ldb={get:e,set:function(e,t){o.k=e,o.v=t,n.transaction("s","readwrite").objectStore("s").put(o)}}}();
tjvv9vkg

tjvv9vkg2#

你不能,它是硬连接在5MB。这是一个design decision by the Chrome developers
在Chrome中,默认情况下Web SQL数据库和缓存清单也有较低的限制,但如果您为Chrome App Store打包应用程序,则可以增加它们。
Managing HTML5 Offline Storage - Google Chrome

goucqfw6

goucqfw63#

你不能,但是如果你把JSON保存在localStorage中,你可以使用一个库来压缩数据,比如:https://github.com/k-yak/JJLC
演示:http://k-yak.github.io/JJLC/

yi0zb3m4

yi0zb3m44#

配额是由 * 用户 * 设置的,他希望允许每个网站有多少空间。
因此,由于目的是限制网页,所以网页不能改变限制。
如果存储空间不足,您可以提示用户增加本地存储空间。
要查明存储是否不足,可以通过保存对象然后删除它来探测本地存储大小。

9gm1akwq

9gm1akwq5#

在这里你可以测试你的程序,你应该也处理的情况下,当cuota超过

3b6akqbq

3b6akqbq6#

https://stackoverflow.com/a/5664344/2630686上面的答案是非常惊人的。我在我的项目中应用了它,并实现了一个完整的解决方案来请求各种资源。

// Firstly reference the above ldb code in the answer I mentioned.
export function get_file({ url, d3, name, enable_request = false }) {
  if (name === undefined) { // set saved data name by url parsing alternatively
    name = url.split('?')[0].split('/').at(-1).split('.')[0];
  }
  const html_name = location.href.split('/').at(-1).split('.')[0]
  name = `${html_name}_${name}`
  let ret = null;
  const is_outer = is_outer_net(url); // check outer net url by its start with http or //
  // try to access data from local. Return null if not found
  if (is_outer && !enable_request) {
    if (localStorage[name]) {
      ret = new Promise(resolve => resolve(JSON.parse(localStorage[name])));
    } else {
      ret = new Promise(r => {
        ldb.get(name, function (value) {
          r(value)
        })
      });
    }
  } else {
    ret = new Promise(r => r(null))
  }
  ret.then(data => {
    if (data) {
      return data
    } else {
      const method = url.split('.').at(-1)
      // d3 method supported
      if (d3 && d3[method]) {
        ret = d3[method](url)
      } else {
        if (url.startsWith('~/')) { // local files accessed supported. You need a local service that can return local file data by requested url's address value
          url = `http://localhost:8010/get_file?address=${url}`
        }
        ret = fetch(url).then(data => {
          // parse data by requested data type
          if (url.endsWith('txt')) {
            return data.text()
          } else {

            return data.json()
          }
        })
      }
      ret = ret.then(da => {
        data = da
        if (is_outer) { // save data to localStorage firstly
          localStorage[name] = JSON.stringify(data);
        }
      }).catch(e => { // save to ldb if 5MB exceed
        ldb.set(name, data);
      }).finally(_ => {

        return data;
      });
    }
  })
  return ret;
}
jaql4c8m

jaql4c8m7#

我也遇到了同样的问题。经过仔细考虑,我认为从localStorage迁移到其他类型的本地存储,如indexedDB,是处理大量数据时更好的方法。
原因是localStorage对阻塞的同步进程进行操作。通常不建议使用同步进程存储大量数据(这可能是Chrome开发人员团队坚决不允许用户或开发人员增加localStorage的5MB硬限制的原因)。
不过,说实话...我们欣赏localStorageAPI的简单性、易用性和直接性。它在这些方面与indexedDB不同。
因此,我建议使用替代库:https://github.com/dreamsavior/Better-localStorage
此库的设计与localStorage类似,使迁移过程变得简单:

const MyLocalStorage = require('better-localstorage');
await myStorage.setItem("someKey", { "someObject": "with the value" });
const value = await myStorage.getItem("someKey");
console.log(value);
  • 请注意,您不需要将JavaScript对象转换为JSON。

使用本机API,您需要执行以下操作:

myStorage.setItem("someKey", JSON.stringify({ "someObject": "with the value" }));
const value = JSON.parse(myStorage.getItem("someKey"));
console.log(value);

相关问题