forked from indexeddbshim/IndexedDBShim
-
Notifications
You must be signed in to change notification settings - Fork 2
/
globalVars.js
87 lines (77 loc) · 3.54 KB
/
globalVars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(function(window, idbModules){
'use strict';
function shim(name, value) {
try {
// Try setting the property. This will fail if the property is read-only.
window[name] = value;
}
catch (e) {}
if (window[name] !== value && Object.defineProperty) {
// Setting a read-only property failed, so try re-defining the property
try {
Object.defineProperty(window, name, {
value: value
});
}
catch (e) {}
if (window[name] !== value) {
window.console && console.warn && console.warn('Unable to shim ' + name);
}
}
}
shim('shimIndexedDB', idbModules.shimIndexedDB);
if (window.shimIndexedDB) {
window.shimIndexedDB.__useShim = function(){
if (typeof window.openDatabase !== "undefined") {
// Polyfill ALL of IndexedDB, using WebSQL
shim('indexedDB', idbModules.shimIndexedDB);
shim('IDBFactory', idbModules.IDBFactory);
shim('IDBDatabase', idbModules.IDBDatabase);
shim('IDBObjectStore', idbModules.IDBObjectStore);
shim('IDBIndex', idbModules.IDBIndex);
shim('IDBTransaction', idbModules.IDBTransaction);
shim('IDBCursor', idbModules.IDBCursor);
shim('IDBKeyRange', idbModules.IDBKeyRange);
shim('IDBRequest', idbModules.IDBRequest);
shim('IDBOpenDBRequest', idbModules.IDBOpenDBRequest);
shim('IDBVersionChangeEvent', idbModules.IDBVersionChangeEvent);
}
else if (typeof window.indexedDB === "object") {
// Polyfill the missing IndexedDB features
idbModules.polyfill();
}
};
window.shimIndexedDB.__debug = function(val){
idbModules.DEBUG = val;
};
}
// Workaround to prevent an error in Firefox
if(!('indexedDB' in window)) {
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.oIndexedDB || window.msIndexedDB;
}
// Detect browsers with known IndexedDb issues (e.g. Android pre-4.4)
var poorIndexedDbSupport = false;
if (navigator.userAgent.match(/Android 2/) || navigator.userAgent.match(/Android 3/) || navigator.userAgent.match(/Android 4\.[0-3]/)) {
/* Chrome is an exception. It supports IndexedDb */
if (!navigator.userAgent.match(/Chrome/)) {
poorIndexedDbSupport = true;
}
}
if ((typeof window.indexedDB === "undefined" || !window.indexedDB || poorIndexedDbSupport) && typeof window.openDatabase !== "undefined") {
window.shimIndexedDB.__useShim();
}
else {
window.IDBDatabase = window.IDBDatabase || window.webkitIDBDatabase;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
window.IDBCursor = window.IDBCursor || window.webkitIDBCursor;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
if(!window.IDBTransaction){
window.IDBTransaction = {};
}
/* Some browsers (e.g. Chrome 18 on Android) support IndexedDb but do not allow writing of these properties */
try {
window.IDBTransaction.READ_ONLY = window.IDBTransaction.READ_ONLY || "readonly";
window.IDBTransaction.READ_WRITE = window.IDBTransaction.READ_WRITE || "readwrite";
} catch (e) {}
}
}(window, idbModules));