Skip to content

Instantly share code, notes, and snippets.

@mzabriskie
mzabriskie / Base64.js
Created April 3, 2013 20:02
Base64 encode/decode for JavaScript
// http://www.webtoolkit.info/javascript-base64.html
(function () {
var KEY_STR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
// private method for UTF-8 encoding
function _utf8_encode(string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
@mzabriskie
mzabriskie / EvalCssText.js
Created April 3, 2013 20:11
Evaluate CSS text to add it to the page.
// Evaluate CSS text
// Can be used to add CSS to the page from XHR response, or localStorage, etc.
function evalCssText(text) {
// When Base64 encoding the data URI CSS urls, such as background images will not load if they aren't fully qualified URLs
// NOTE: Change this to whatever matches your path. All my images are loaded from a /static directory.
text = text.replace(/\/static\//gim, window.location.protocol + '//' + window.location.host + '/static/');
var href = 'data:text/css;base64,' + Base64.encode(text);
var node = new Element('link');
node.set('type', 'text/css');
@mzabriskie
mzabriskie / random-token.js
Created September 9, 2013 22:39
Base 36 random token
Math.floor(Math.random() * Math.pow(2, 64)).toString(36);
/**
* Creates an array containing a range of values.
*
* <p>
* Examples:
*
* <code>
* // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
* Array.range(1, 10);
*
function duration(timeInMillis) {
var intervals = {
'h': 3600000,
'm': 60000,
's': 1000,
'ms': 1
},
sb = '';
for (var k in intervals) {
@mzabriskie
mzabriskie / README.md
Last active February 5, 2024 15:10
Check git status of multiple repos

If you're like me you have a dir like ~/Workspace/Github where all your git repos live. I often find myself making a change in a repo, getting side tracked and ending up in another repo, or off doing something else all together. After a while I end up with several repos with modifications. This script helps me pick up where I left off by checking the status of all my repos, instead of having to check each one individually.

Usage:

git-status [directory]

This will run git status on each repo under the directory specified. If called with no directory provided it will default to the current directory.

@mzabriskie
mzabriskie / getElementLegacy.js
Last active August 29, 2015 13:56
Legacy get element code circa 2001
function getElement(id) {
var el = null;
if (document.getElementById) {
el = document.getElementById(id);
}
else if (document.all) {
el = document.all[id];
}
else if (document.layers) {
el = document.layers[id];

Keybase proof

I hereby claim:

  • I am mzabriskie on github.
  • I am mzabriskie (https://keybase.io/mzabriskie) on keybase.
  • I have a public key whose fingerprint is 2B8A A1C7 D943 8BB3 7D38 C760 ECF6 90AD 2240 A96B

To claim this, I am signing this object:

[].slice.call(document.querySelectorAll('tr.paper')).sort(function (a, b) {
a = parseInt(a.querySelector('td:first-of-type span.score').innerHTML);
b = parseInt(b.querySelector('td:first-of-type span.score').innerHTML);
return a > b ? -1 : b > a ? 1 : 0;
}).forEach(function (node) {
node.parentNode.appendChild(node);
});
@mzabriskie
mzabriskie / Gamepad.md
Last active August 29, 2015 14:02
Discussion for how to best implement Gamepad support

gamepad.js

I haven't been able to find any libraries for interacting with the HTML5 Gamepad API that I really like. This is an attempt to sort out some of my ideas for what I would want out of such a library and ultimately implement it.

Goals

  • Abstract away complexity
    • Manage gamepad support (detection, vender impl, polling, etc.)
    • Make it dead simple for developers to implement