Skip to content

Instantly share code, notes, and snippets.

@mrcoles
mrcoles / safekeypress.jquery.js
Created April 1, 2012 19:45
A reliable jQuery keypress event especially for arrow keys
$.fn.safekeypress = function(func, cfg) {
cfg = $.extend({
stopKeys: {37:1, 38:1, 39:1, 40:1}
}, cfg);
function isStopKey(evt) {
var isStop = (cfg.stopKeys[evt.keyCode] || (cfg.moreStopKeys && cfg.moreStopKeys[evt.keyCode]));
if (isStop) evt.preventDefault();
return isStop;
@mrcoles
mrcoles / PressedKeys.js
Created April 11, 2012 04:46
A simple way to keep track of which keys are pressed at any one time.
var PressedKeys = function($elt) {
var pressed = {};
($elt || $(document)).on('keydown keyup', function(evt) {
pressed[evt.keyCode] = (evt.type == 'keydown');
});
return pressed;
};
// example
@mrcoles
mrcoles / mkdjangovirtualenv.sh
Created October 19, 2012 21:26
A bash script to setup various settings for a django virtualenv
#!/usr/bin/env bash
#
# Make a virtualenv specifically for a django project using virtualenvwrapper
#
# * Set the project directory in the .project file
# * Set the DJANGO_SETTINGS_MODULE and PYTHONPATH in postactivate
# * Install pip requirements if there's a requirements.txt file
# * Install git submodules if a git repo with submodules
#
@mrcoles
mrcoles / bson_pprint.py
Created November 14, 2012 21:07
A tool for pretty printing bson string (e.g., output of mongoexport)
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from bson import json_util, ObjectId
from bson.py3compat import string_types
from bson.dbref import DBRef
DEFAULT_INDENT = 4
@mrcoles
mrcoles / imports.less
Last active December 11, 2015 21:09
meteor less test example
@foo: cyan;
@mrcoles
mrcoles / detect-autoplay.js
Created May 8, 2013 01:25
A script to detect browser support for the autoplay attribute on the HTML5 Audio element.
// Detect autoplay
// ---------------
// This script detects whether the current browser supports the
// autoplay feature for HTML5 Audio elements, and it sets the
// `AUTOPLAY` variable accordingly.
// Used in the Meteor app [PicDinner](http://picdinner.com)

Keybase proof

I hereby claim:

  • I am mrcoles on github.
  • I am mrcoles (https://keybase.io/mrcoles) on keybase.
  • I have a public key whose fingerprint is 8832 F369 A11A 05DE B8A6 A2C1 977D 1CDA 6C93 9216

To claim this, I am signing this object:

@mrcoles
mrcoles / storage.py
Created April 23, 2015 01:22
StaticCachedS3BotoStorage - that reads its manifest.json locally
from django.conf import settings
from django.core.files.storage import get_storage_class
from compressor.cache import get_offline_manifest_filename
from storages.backends.s3boto import S3BotoStorage
class StaticCachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that also saves files locally for django-compressor
@mrcoles
mrcoles / unirest_promise.js
Created September 8, 2017 17:25
Node Unirest Promise Wrapper
function unirest_prom(unirest_req, always_resolve) {
// Returns a Promise by wrapping a unirest.Request object in
// a Promise that immediately calls `.end(...)`
//
// Params:
//
// * unirest_req - unirest.Request - any unirest Request object that has
// not yet had `.end(...)` called on it
// * always_resolve - bool (optional) - defaults to `false`, iff `true` then
// the Promise always resolves--even when the request fails
@mrcoles
mrcoles / replace_words.js
Last active February 25, 2022 05:41
Replace all instances of one word with another in a web page
// ### Replace words in document
//
// Update all instances of `fromWord` to `toWord` within the text
// in the current document.
//
function replaceWordsInDocument(fromWord, toWord) {
if (/\s/.test(fromWord)) {
throw new Error('You must enter a single word without whitespace');
}