User:Manishearth/library.js

/* A library of functions which I might like to use in my userscripts or while experimenting on WP with Chrome's js console I WILL NOT import this humongous library onto scripts, rather I'll use it while testing and copypaste the required functions



//CSS stuff function getCSSRule(ruleName, deleteFlag) { ruleName=ruleName.toLowerCase; if (document.styleSheets) { for (var i=0; i<document.styleSheets.length; i++) { var styleSheet=document.styleSheets[i]; var ii=0; var cssRule=false; do { if (styleSheet.cssRules) { cssRule = styleSheet.cssRules[ii]; } else { cssRule = styleSheet.rules[ii]; }                                               if (cssRule)  { if (cssRule.selectorText.toLowerCase==ruleName) { if (deleteFlag=='delete') { if (styleSheet.cssRules) { styleSheet.deleteRule(ii); } else { styleSheet.removeRule(ii); }                                                return true; } else { return cssRule; }                                             }                                              }                                                 ii++; } while (cssRule) }                                             }                                                 return false; }

function killCSSRule(ruleName) { return getCSSRule(ruleName,'delete'); }

function addCSSRule(ruleName) { if (document.styleSheets) { if (!getCSSRule(ruleName)) { if (document.styleSheets[0].addRule) { document.styleSheets[0].addRule(ruleName, null,0); } else { document.styleSheets[0].insertRule(ruleName+' { }', 0); }           }             }                return getCSSRule(ruleName); }

/*SANITIZATION: //adds a trim function String.prototype.trim = function { return this.replace(/^\s+|\s+$/g,""); } String.prototype.ltrim = function { return this.replace(/^\s+/g,""); } String.prototype.rtrim = function { return this.replace(/\s+$/g,""); } //Format numbers with commas function formatNumber(num,prefix){ prefix = prefix || ''; num += ''; var splitStr = num.split('.'); var splitLeft = splitStr[0]; var splitRight = splitStr.length > 1 ? '.' + splitStr[1] : ''; var regx = /(\d+)(\d{3})/; while (regx.test(splitLeft)) { splitLeft = splitLeft.replace(regx, '$1' + ',' + '$2'); }  return prefix + splitLeft + splitRight; }

function unformatNumber(num) { return num.replace(/([^0-9\.\-])/g,'')*1; }

//sanitizes HTML inputs String.prototype.htmlEntities = function { return this.replace(/&/g,'&amp;').replace(//g,'&gt;'); }

//removes HTML tags String.prototype.stripTags = function { return this.replace(/<([^>]+)>/g,''); }

/* LAZINESS: */ //numeric sort for arrays--true laziness Array.prototype.sortNum = function { return this.sort( function (a,b) { return a-b; } ); }

//run a function on all elmts of array (mapped) Array.prototype.map = function(f) { var returnArray=[]; for (i=0; i<this.length; i++) { returnArray.push(f(this[i])); } return returnArray; } //is it an array? function isArray(testObject) { return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number'; }

/* ALL SCRIPTS BELOW THIS LINE TAKEN FROM http://www.dustindiaz.com/top-ten-javascript/ I have made a few changes, but they're more or less intact

//shortened copy of getElmtById, except it takes care of multiple elements

function $ID { var elements = new Array; for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') element = document.getElementById(element); if (arguments.length == 1) return element; elements.push(element); }	return elements; } var $=$ID

//Cookie functions function getCookie( name ) { var start = document.cookie.indexOf( name + "=" ); var len = start + name.length + 1; if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return null; }	if ( start == -1 ) return null; var end = document.cookie.indexOf( ';', len ); if ( end == -1 ) end = document.cookie.length; return unescape( document.cookie.substring( len, end ) ); }

function setCookie( name, value, expires, path, domain, secure ) { var today = new Date; today.setTime( today.getTime ); if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }	var expires_date = new Date( today.getTime + (expires) ); document.cookie = name+'='+escape( value ) + ( ( expires ) ? ';expires='+expires_date.toGMTString : '' ) + //expires.toGMTString ( ( path ) ? ';path=' + path : '' ) + ( ( domain ) ? ';domain=' + domain : '' ) + ( ( secure ) ? ';secure' : '' ); }

function deleteCookie( name, path, domain ) { if ( getCookie( name ) ) document.cookie = name + '=' + ( ( path ) ? ';path=' + path : '') + ( ( domain ) ? ';domain=' + domain : '' ) + ';expires=Thu, 01-Jan-1970 00:00:01 GMT'; }

//inArray proto Array.prototype.inArray = function (value) { var i;	for (i=0; i < this.length; i++) { if (this[i] === value) { return true; }	}	return false; }

//insertAfter to mirror insertBefore function insertAfter(parent, node, referenceNode) { parent.insertBefore(node, referenceNode.nextSibling); }

//toggle to toggle an elements display attribute function toggle(obj) { var el = document.getElementById(obj); if ( el.style.display != 'none' ) { el.style.display = 'none'; }	else { el.style.display = ''; } }

//PET PEEVE SOLVER (aka getElmtsByClass) function getElementsByClass(searchClass,node,tag) { var classElements = new Array; if ( node == null ) node = document; if ( tag == null ) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)'); for (i = 0, j = 0; i < elsLen; i++) { if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; }	}	return classElements; }

//My shortcut for this one, to mirror $ var _ = getElementsByClass;