Last active
June 5, 2016 09:30
-
-
Save nolanlawson/8a2ead46a184c9fae231 to your computer and use it in GitHub Desktop.
Test for broken implementation of IndexedDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<h1>Test for broken IndexedDB</h1> | |
<p>This page tests the current browser's IndexexDB to see if it contains bugs.</p> | |
<p>Possible bugs you might see: | |
<ul> | |
<li>IndexedDB not supported at all</li> | |
<li>Samsung/HTC bug where IndexedDB doesn't have <code>onupgradeneeded</code></li> | |
<li>Apple bug in UIWebView on iOS 8 where IDBKeyRange is null rather than undefined</li> | |
<li>Apple bug in desktop Safari 7.1+ and iOS 8+ Safari where IndexedDB doesn't allow opening two databases at once and is basically unusable.</li> | |
</ul> | |
</p> | |
<pre id="display"></pre> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var display = document.getElementById('display'); | |
function log(str) { | |
display.innerHTML = (display.innerHTML || '') + '\n' + str; | |
} | |
if (typeof indexedDB === 'undefined') { | |
log('This browser doesn\'t have IndexedDB.'); | |
} else if (window.indexedDB === null) { | |
log('This browser has the Apple UIWebView bug where indexedDB is null instead of undefined.'); | |
} else if (typeof IDBKeyRange === 'undefined') { | |
log('This browser has Samsung/HTC\'s broken version of IndexedDB (indexedDB is defined but not IDBKeyRange).') | |
} else { | |
testAppleBrokenIndexedDB(); | |
} | |
function testAppleBrokenIndexedDB() { | |
var req = indexedDB.open('test', 1); | |
req.onupgradeneeded = function (e) { | |
var db = e.target.result; | |
db.createObjectStore('one', { | |
keyPath: 'key' | |
}); | |
db.createObjectStore('two', { | |
keyPath: 'key' | |
}); | |
}; | |
req.onerror = function () { | |
}; | |
req.onsuccess = function (e) { | |
var db = e.target.result; | |
var tx; | |
try { | |
tx = db.transaction(['one', 'two'], 'readwrite'); | |
} catch (err) { | |
log('This browser has Apple\'s broken implementation of IndexedDB (can\'t open two DBs at once).'); | |
return; | |
} | |
tx.oncomplete = function (e) { | |
db.close(); | |
log('This browser has a working implementation of IndexedDB.') | |
}; | |
var req = tx.objectStore('two').put({ | |
'key': new Date().valueOf() | |
}); | |
req.onsuccess = function (e) { | |
}; | |
req.onerror = function () { | |
}; | |
}; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment