Skip to content

Instantly share code, notes, and snippets.

@robnyman
robnyman / blob-filereader-localStorage.js
Last active April 21, 2024 08:32
Get file as a blob, read through FileReader and save in localStorage
// Getting a file through XMLHttpRequest as an arraybuffer and creating a Blob
var rhinoStorage = localStorage.getItem("rhino"),
rhino = document.getElementById("rhino");
if (rhinoStorage) {
// Reuse existing Data URL from localStorage
rhino.setAttribute("src", rhinoStorage);
}
else {
// Create XHR and FileReader objects
var xhr = new XMLHttpRequest(),
@robnyman
robnyman / Create-open-IndexedDB-database.js
Created February 23, 2012 15:50
Create/open IndexedDB database
// IndexedDB
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction,
dbVersion = 1;
/*
Note: The recommended way to do this is assigning it to window.indexedDB,
to avoid potential issues in the global scope when web browsers start
removing prefixes in their implementations.
@robnyman
robnyman / Create-IndexedDB-objectStore.js
Created February 23, 2012 16:08
Create IndexedDB objectStore
// Create an objectStore
console.log("Creating objectStore")
dataBase.createObjectStore("elephants");
@robnyman
robnyman / xhr-BlobBuilder.js
Created February 23, 2012 16:24
xhr-BlobBuilder
// Create XHR
var xhr = new XMLHttpRequest(),
blob;
xhr.open("GET", "elephant.png", true);
// Set the responseType to blob
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
@robnyman
robnyman / Open-IndexedDB-transaction.js
Created February 23, 2012 16:29
Open IndexedDB transaction
// Open a transaction to the database
var transaction = db.transaction(["elephants"], IDBTransaction.READ_WRITE);
@robnyman
robnyman / Put-blob-into-IndexedDB-database.js
Created February 23, 2012 16:32
Put blob into IndexedDB database
// Put the blob into the dabase
transaction.objectStore("elephants").put(blob, "image");
@robnyman
robnyman / retrieve-indexeddb-file-create-object-url.js
Created February 23, 2012 17:28
Retrieve stored file from IndexedDB and create an ObjectURL
// Retrieve the file that was just stored
transaction.objectStore("elephants").get("image").onsuccess = function (event) {
var imgFile = event.target.result;
console.log("Got elephant!" + imgFile);
// Get window.URL object
var URL = window.URL || window.webkitURL;
// Create and revoke ObjectURL
var imgURL = URL.createObjectURL(imgFile);
@robnyman
robnyman / IndexedDB-storing-and-retrieving-files.js
Created February 23, 2012 17:55
IndexedDB storing and retrieving files
(function () {
// IndexedDB
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.OIDBTransaction || window.msIDBTransaction,
dbVersion = 1.0;
// Create/open database
var request = indexedDB.open("elephantFiles", dbVersion),
db,
createObjectStore = function (dataBase) {
@robnyman
robnyman / WebTelephony-API.js
Created March 1, 2012 17:32
WebTelephony API
// Telephony object
var tel = navigator.mozTelephony;
// Check if the phone is muted (read/write property)
console.log(tel.muted);
// Check if the speaker is enabled (read/write property)
console.log(tel.speakerEnabled);
// Place a call
@robnyman
robnyman / WebSMS-API.js
Created March 1, 2012 17:41
WebSMS API
// SMS object
var sms = navigator.mozSMS;
// Send a message
sms.send("123456789", "Hello world!");
// Recieve a message
sms.onrecieved = function (event) {
// Read message
console.log(event.message);