Skip to content

Instantly share code, notes, and snippets.

@mjlescano
mjlescano / asyncCss.js
Last active August 29, 2015 14:04
Global asyncCss() function to load async stylesheets, can accept callback as second argument that will be executed when all the stylesheets had finished loading. (IE6 compatible in theory.)
;(function(w,d){
var rAF = w.requestAnimationFrame
|| w.mozRequestAnimationFrame
|| w.webkitRequestAnimationFrame
|| w.msRequestAnimationFrame
|| function(f){ f() }
var callbacks = []
var pending = 0
var hasOnload = 'onload' in d.createElement('link')
@mjlescano
mjlescano / fbReady.js
Last active August 29, 2015 14:04
fbReady Function, to execute things when facebook asynchronously loaded
;(function(){
// Function to have a list of functions to load on fbAsyncInit
var toLoad = []
window.fbReady = function(func){
window.FB ? func.call(window) : toLoad.push(func)
}
window.fbAsyncInit = function() {
FB.init({
appId: "#{FACEBOOK_API}",
status: true,
@mjlescano
mjlescano / jquery.event.stop.js
Last active August 29, 2015 14:07
jQuery event.preventDefault() and event.stopPropagation() shortcut.
/**
jQuery.Event.stop extension
===========================
Simple shortcut to do:
$(document).on('click', 'a', function(e){
return e.stop()
})
@mjlescano
mjlescano / jquery.openSelect.js
Created October 21, 2014 13:48
Plugin to open the dropdown of a select element
/*
* jQuery openSelect
*
* Matías Lescano | @touteo
* Licensed under the MIT license
*/
;(function($){
$.fn.openSelect = function(options){
return this.each(function(){
@mjlescano
mjlescano / share.js
Created October 22, 2014 01:40
Javascript helpers to share urls programatically, on facebook or twitter
var S = module.exports = {}
/**
* If the page has og:meta tags facebook will parse the page and read them.
* So the options are optionals.
*/
S.onFacebook = function(options, callback){
// New implementation
// https://developers.facebook.com/docs/reference/dialogs/feed/
if( callback ) {
@mjlescano
mjlescano / svg-fallback.html
Created November 6, 2014 15:18
Fallback for .svg <img>, tries to load the .png version.
<img src="sarasa.svg" onerror="this.onerror=null;this.src=this.src.replace(/.svg$/, '.png')">
@mjlescano
mjlescano / loadCachedCss.js
Last active August 29, 2015 14:10
Synchronous css loading, but caching it on localStorage. Based on https://gist.github.com/hdragomir/8f00ce2581795fd7b1b7, to be used on http://goodpeople.com
;(function(w, d){
var rAF = w.requestAnimationFrame
|| w.mozRequestAnimationFrame
|| w.webkitRequestAnimationFrame
|| w.msRequestAnimationFrame
|| function(f){ setTimeout(f, 0) }
var s = window.localStorage
var available = null
@mjlescano
mjlescano / css-guides.scss
Created December 31, 2014 04:40
Guides for working with css. Mixins to creates guides like the ones from photoshop.
@mixin css-guide {
content: '';
position: absolute;
opacity: 0.3;
background-color: cyan;
}
@mixin css-guide-h {
@include css-guide;
left: 0;
@mjlescano
mjlescano / waitFor.js
Last active August 29, 2015 14:15
Silly lib that executes a given callback when a variable is defined on window.
/**
* waitFor.js
*
* Silly lib that executes a given callback when a variable is defined on window.
*
* Matías Lescano | @mjlescano
* Licensed under the MIT license
*/
;if( !window.waitFor ) (function(w){
@mjlescano
mjlescano / retry
Last active August 29, 2015 14:20 — forked from dansimau/gist:842415
Bash function for running a command, checking the return code, and re-trying it `x` times after `y` sleep seconds.
#!/bin/bash
# Usage:
# retry <commands...>
# retry <retry times> <commands...>
# retry <retry times> <retry wait> <commands...>
if [[ $2 =~ ^-?[0-9]+$ ]]; then
cmd="${@:3}"
retry_times=$1