Skip to content

Instantly share code, notes, and snippets.

@cauerego
cauerego / IndexedDB101.js
Last active January 13, 2023 22:00 — forked from JamesMessinger/IndexedDB101.js
Very Simple IndexedDB Example
// quite untested, adapted from BigstickCarpet's gist, attempt to make it simpler to use
function openIndexedDB (fileindex) {
// This works on all devices/browsers, and uses IndexedDBShim as a final fallback
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
var openDB = indexedDB.open("MyDatabase", 1);
openDB.onupgradeneeded = function() {
var db = {}
@ijprest
ijprest / convert-to-amd.js
Created July 20, 2015 23:49
Very simple script to convert a normal JS module to a RequireJS-compatible AMD module; intended for command-line usage, and integration into build systems.
#!/usr/bin/env node
///
/// Very simple script to convert a normal JS module to a RequireJS-compatible
/// AMD module; intended for command-line usage, and integration into build
/// systems.
///
/// Reads from stdin & writes to stdout; output can be piped to uglifyjs to
/// minify/compress the code.
///
/// Examples:
@malthe
malthe / dedent.js
Created December 16, 2014 11:25
This is similar to Python's "textwrap.dedent" function
function dedent(text) {
var re_whitespace = /^([ \t]*)(.*)\n/gm;
var l, m, i;
while ((m = re_whitespace.exec(text)) !== null) {
if (!m[2]) continue;
if (l = m[1].length) {
i = (i !== undefined) ? Math.min(i, l) : l;
} else break;
@rmehner
rmehner / delete-databases.js
Last active April 25, 2026 22:09
Delete all indexedDB databases
// Credit to @steobrien from https://gist.github.com/rmehner/b9a41d9f659c9b1c3340#gistcomment-2940034
// for modern browsers, this works:
const dbs = await window.indexedDB.databases()
dbs.forEach(db => { window.indexedDB.deleteDatabase(db.name) })
// for older browsers, have a look at previous revisions of this gist.
@ibrechin
ibrechin / jsspectrum.js
Created April 25, 2012 11:13
Generate evenly distributed spectrum in JavaScript
hslToRgb = function(_h, s, l) {
var h = Math.min(_h, 359)/60;
var c = (1-Math.abs((2*l)-1))*s;
var x = c*(1-Math.abs((h % 2)-1));
var m = l - (0.5*c);
var r = m, g = m, b = m;
if (h < 1) {
@gleuch
gleuch / gist:2475825
Created April 24, 2012 02:58
Javascript documentfragment to string (w/ text selection)
// selection range
var range = window.getSelection().getRangeAt(0);
// plain text of selected range (if you want it w/o html)
var text = window.getSelection();
// document fragment with html for selection
var fragment = range.cloneContents();
// make new element, insert document fragment, then get innerHTML!