我使用Safari12.1,使用javascript开发IndexedDB。我需要获取所有indexedDB数据库名称,但safari不支持indexedDB.databases()函数

虽然它在chrome中得到了支持。

那么如何在Safari中获取所有的indexedDB数据库呢?
请帮帮忙。
发布于 2019-09-04 19:55:37
发布于 2020-09-11 02:22:40
/**
* 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);
};
}
})();发布于 2021-09-01 21:58:48
@jamesmfriedman的解决方案在Firefox中为我抛出了错误,当IF语句被禁用时,也会在Chrome中抛出错误。
我偶然发现了一个拦截方法调用的代理方法(来自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);
});
// }
}());
https://stackoverflow.com/questions/57787209
复制相似问题