Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save nolanlawson/11b1af329e1f8f27fa13 to your computer and use it in GitHub Desktop.

Select an option

Save nolanlawson/11b1af329e1f8f27fa13 to your computer and use it in GitHub Desktop.
Tests gifs in WebSQL 2
<html>
<body>
<h1>Load a bunch of images into PouchDB</h1>
<script src="pouchdb.js"></script>
<script src="index.js"></script>
</body>
</html>
document.addEventListener('DOMContentLoaded', function () {
'use strict';
var db;
function createBlob(parts, properties) {
parts = parts || [];
properties = properties || {};
try {
return new Blob(parts, properties);
} catch (e) {
if (e.name !== "TypeError") {
throw e;
}
var BlobBuilder = window.BlobBuilder ||
window.MSBlobBuilder ||
window.MozBlobBuilder ||
window.WebKitBlobBuilder;
var builder = new BlobBuilder();
for (var i = 0; i < parts.length; i += 1) {
builder.append(parts[i]);
}
return builder.getBlob(properties.type);
}
}
function onGetBlob(imageSrc, blob) {
db.putAttachment(imageSrc, imageSrc, null, blob, blob.type).then(function () {
return db.getAttachment(imageSrc, imageSrc);
}).then(function (blob) {
var blobUrl = (window.URL || window.webkitURL).createObjectURL(blob);
var img = new Image();
img.src = blobUrl;
document.body.appendChild(img);
});
}
function loadImages() {
var images = ['licking.gif', 'swagger.gif', 'suspicion.gif', 'chopsticks.gif', 'koffing.gif'];
images.forEach(function (src) {
var img = new Image();
img.onload = function () {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var type = xhr.getResponseHeader('content-type');
// convert from arraybuffer to blob
var blob = createBlob([xhr.response || ''], {
type: type
});
onGetBlob(src, blob);
}
};
xhr.open('GET', src, true);
xhr.responseType = 'arraybuffer'; // 'blob' is not supported in Android <4.4
xhr.send();
}
img.src = src;
document.body.appendChild(img);
})
}
new PouchDB('images', {adapter: 'websql'}).destroy().then(function () {
db = new PouchDB('images', {adapter: 'websql'});
loadImages();
});
});
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment