Safari不支持indexedDB.数据库()

dojqjjoe  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(3)|浏览(406)

我正在使用Safari 12.1,并使用javascript在IndexedDB上工作。我需要获取所有indexedDB数据库名称,但Safari不支持indexedDB.databases()函数

,而chrome

支持它
那么如何在Safari中获取所有indexedDB数据库呢?
请帮帮忙。

voj3qocg

voj3qocg1#

在2019年8月29日发布的Safari v13.0.1上看起来更好,所以试试Safari技术预览版吧。
请检查:
https://caniuse.com/#feat=indexeddb
safari
https://bugs.webkit.org
https://github.com/dfahlander/Dexie.js/issues

zzoitvuj

zzoitvuj2#

/**
 * Polyfill for indexedDB.databases()
 * Safari and some other older browsers that support indexedDB do NOT
 * Support enumerating existing databases. This is problematic when it
 * comes time to cleanup, otherwise we could litter their device with
 * unreferenceable database handles forcing a nuclear browser clear all history.
 */

(function () {
    if (window.indexedDB && typeof window.indexedDB.databases === 'undefined') {
        const LOCALSTORAGE_CACHE_KEY = 'indexedDBDatabases';

        // Store a key value map of databases
        const getFromStorage = () =>
            JSON.parse(window.localStorage[LOCALSTORAGE_CACHE_KEY] || '{}');

        // Write the database to local storage
        const writeToStorage = value =>
            (window.localStorage[LOCALSTORAGE_CACHE_KEY] = JSON.stringify(value));

        IDBFactory.prototype.databases = () =>
            Promise.resolve(
                Object.entries(getFromStorage()).reduce((acc, [name, version]) => {
                    acc.push({ name, version });
                    return acc;
                }, [])
            );

        // Intercept the existing open handler to write our DBs names
        // and versions to localStorage
        const open = IDBFactory.prototype.open;

        IDBFactory.prototype.open = function (...args) {
            const dbName = args[0];
            const version = args[1] || 1;
            const existing = getFromStorage();
            writeToStorage({ ...existing, [dbName]: version });
            return open.apply(this, args);
        };

        // Intercept the existing deleteDatabase handler remove our
        // dbNames from localStorage
        const deleteDatabase = IDBFactory.prototype.deleteDatabase;

        IDBFactory.prototype.deleteDatabase = function (...args) {
            const dbName = args[0];
            const existing = getFromStorage();
            delete existing[dbName];
            writeToStorage(existing);
            return deleteDatabase.apply(this, args);
        };
    }
})();
oprakyz7

oprakyz73#

来自@jamesmfriedman的解决方案在Firefox中为我抛出了错误,当IF语句被禁用时,在Chrome中也会抛出错误。
我偶然发现了一个拦截方法调用的PROXY方法(来自https://javascript.plainenglish.io/javascript-how-to-intercept-function-and-method-calls-b9fd6507ff02),并将其集成到polyfill中。截至2021-09-01,这段代码在Chrome和Firefox中都能很好地工作。

/**
 * Polyfill for indexedDB.databases()
 * Safari and some other older browsers that support indexedDB do NOT
 * Support enumerating existing databases. This is problematic when it
 * comes time to cleanup, otherwise we could litter their device with
 * unreferenceable database handles forcing a nuclear browser clear all history.
 */

// eslint-disable-next-line func-names
(function () {
  // if (window.indexedDB && typeof window.indexedDB.databases === 'undefined') {
  const LOCALSTORAGE_CACHE_KEY = 'indexedDBDatabases';

  // Helper function from plainenglish.io to use a proxy to intercept.
  // Original at https://javascript.plainenglish.io/javascript-how-to-intercept-function-and-method-calls-b9fd6507ff02
  const interceptMethodCalls = (obj, fnName, fn) => new Proxy(obj, {
    get(target, prop) {
      if (prop === fnName && typeof target[prop] === 'function') {
        return new Proxy(target[prop], {
          apply: (target2, thisArg, argumentsList) => {
            fn(prop, argumentsList);
            return Reflect.apply(target2, thisArg, argumentsList);
          },
        });
      }
      return Reflect.get(target, prop);
    },
  });

  // Store a key value map of databases
  const getFromStorage = () => JSON.parse(window.localStorage[LOCALSTORAGE_CACHE_KEY] || '{}');

  // Write the database to local storage
  const writeToStorage = (value) => {
    window.localStorage[LOCALSTORAGE_CACHE_KEY] = JSON.stringify(value);
  };

  IDBFactory.prototype.databases = () => Promise.resolve(
    Object.entries(getFromStorage()).reduce((acc, [name, version]) => {
      acc.push({ name, version });
      return acc;
    }, []),
  );

  // Intercept the existing open handler to write our DBs names
  // and versions to localStorage
  interceptMethodCalls(IDBFactory.prototype, 'open', (fnName, args) => {
    const dbName = args[0];
    const version = args[1] || 1;
    const existing = getFromStorage();
    writeToStorage({ ...existing, [dbName]: version });
  });

  // Intercept the existing deleteDatabase handler remove our
  // dbNames from localStorage
  interceptMethodCalls(IDBFactory.prototype, 'deleteDatabase', (fnName, args) => {
    const dbName = args[0];
    const existing = getFromStorage();
    delete existing[dbName];
    writeToStorage(existing);
  });

  // }
}());

相关问题