Skip to content

Instantly share code, notes, and snippets.

@aradnom
aradnom / youtube-embed.js
Created July 22, 2013 17:57
Simple script for embedding multiple youtube videos via the JS API. Allows video objects to be queued as video params along with a callback on state change (useful for pushing tracking events).
var videos = [],
tag = document.createElement( 'script' );
// Load the API
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName( 'script' )[0];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
// Enqueue new youtube video for loading and event tracking
@aradnom
aradnom / grouped-thousands.js
Last active December 20, 2015 21:18
JS for formatting large numbers with commas ('12,345,678'). Uses reduceRight from http://underscorejs.org.
_.reduceRight( num.toString().split('').reverse(), function ( stack, next, i ) { return stack + ( i % 3 == 0 && i > 0 ? next + ',' : next ); }, '' )
@aradnom
aradnom / filter-craigslist-results.js
Created November 4, 2013 08:10
It bugs the hell out of me that you can't conveniently filter Craigslist search results for only things in a specific city. This filters out trash rows based on criteria in the regex.
$( $.grep( $('#toc_rows .row'), function ( el ) { return /Sparks|Carson|Tahoe|Zephyr\sCove|Tahoma|Genoa|Wingfield|Vizcaya|Dayton|Gardnerville|Stead|Stagecoach|Fernley|Stateline|Silver\sSprings|Lovelock/gi.test( $(el).find('.l2 .pnr small').text() ); } ) ).remove()
@aradnom
aradnom / visible-in-container.js
Created November 5, 2013 07:43
Small jQuery extension for filtering array of items by visibility in container (not just DOM visibility, but visible to user in the specified container). Filters based on left and right boundaries only.
// Return array of items visible in container. This means items which are not
// only viewable in the DOM, but also viewable to the user (inside the container).
// Note this will only check left and right boundaries and also that it will return
// elements which are partially visible (one foot in the door).
$.fn.visibleInContainer = (function ( container ) {
var left = container.offset().left,
right = left + container.outerWidth();
var visible = $.grep( this, function ( el ) {
var el_left = $(el).offset().left,
@aradnom
aradnom / SoundCloud Player
Created January 27, 2014 19:48
Chunk of JS from a larger project illustrating how to use the SoundCloud API to construct a basic player with playlist, seek and basic control functionality.
// Initialize the Soundcloud API and set up player functionality
Soundcloud: {
// Start the API up
Init: function () {
SC.initialize({
client_id: app.Config.soundcloudClient
});
// For each audio player, bind soundcloud functionality
$('.audio-player').each( function () {
@aradnom
aradnom / round-to.js
Created February 20, 2014 00:47
Round number to nearest <n> (5, 10, 50, etc.)
// Round value to nearest <n>
// multiplier allows values to be multiplied and stay rounded to nearest <n>
// (i.e. 1732 * 2 => nearest 50)
function RoundTo ( value, to, multiplier ) {
multiplier = multiplier || 1;
return Math.round( ( parseInt( value ) * multiplier ) / to ) * to;
}
@aradnom
aradnom / ellipsize.js
Created February 21, 2014 23:44
Ellipsize text in a container with elements. Note that the base function only looks at text, so it's possible to split an inline element. Example of use included.
// Example of ellipsizing
$( function () {
var ellipsis_length = 10;
// Ellipsize text
var text_elements = $('.promotion__content__text *');
// Flip through content elements looking for ellipsis boundary
var boundary = null;
text_elements.each( function() {
@aradnom
aradnom / wp-clear-all-crons.php
Created March 28, 2014 16:33
Clear all crons in WordPress. This is a nuclear option, so use with care.
// Pull all crons from options
$crons = get_option('cron');
// Kill each one. Filter by timestamp or hook_name if desired.
foreach ($crons as $timestamp => $hook) {
foreach ($hook as $hook_name => $details) {
wp_unschedule_event( $timestamp, $hook_name );
}
}
@aradnom
aradnom / grunticon-svg-inliner.js
Last active August 29, 2015 14:03
Grunticon SVG inliner - automatically replace grunticon-produced SVG backgrounds with inline SVGs. Framework-agnostic.
(function () {
var waiting = setInterval( function () {
var nodes = document.querySelectorAll('.inline-svg[class^="icon-"], .inline-svg[class*=" icon-"]');
for ( var i = 0; i < nodes.length; i++ ) {
// Pull the background in (this should always be a background image)
var background = document.defaultView.getComputedStyle(nodes[i])['background-image'];
// Check to see if an element has a background - if so, background has loaded
if (background && background !== 'none') {
if ( waiting ) clearInterval( waiting );
@aradnom
aradnom / jscrollpane-mouseup-fix.js
Created July 11, 2014 23:31
jScrollPane is a neato custom scrollbar plugin, but it won't unbind drag mouseup events correctly within a Google Maps container (it binds these events to html and the unbind never fires). The following resolves this.
$('.jscrollpane__container').jScrollPane().bind( 'mouseup.jsp', function () {
$('html').unbind('dragstart.jsp selectstart.jsp mousemove.jsp mouseup.jsp mouseleave.jsp');
// This will remove the active class from ALL windows, so keep that in mind
// if that isn't the desired behavior.
$('.jspActive').removeClass( 'jspActive' );
});