Old code: CSS rules on the fly from JavaScript (2006)

Again, some old code I'm not using anymore since I switched to jQuery but that might be useful for someone:

/*
 * Public Domain
 * Created by Toni Corvera <outlyer@outlyer.net> in September, 2006
 *
 * Creates a new CSS ruleset, should work with multiple rules correctly
 * e.g.:
 *    loadCssRules('body{font-family:sans-serif;}a:link{color:red;}');
 */
// void function loadCssRules(String rules)
function loadCssRules(r) {
	if (!document.createElement) return;
	var newStyle=document.createElement('STYLE');
	newStyle.setAttribute('type', 'text/css');
	newStyle.appendChild(document.createTextNode(r));
	document.getElementsByTagName('HEAD')[0].appendChild(newStyle);
}

Downloadable file: http://p.outlyer.net/graveyard/net_outlyer_dyn_css.js

Old code: DOM extensions (2004-2006)

Nowadays I'm using something more powerful like jQuery or base2.DOM, but if you don't, these might prove useful.

/*
 * Copyright 2004-2006 Toni Corvera <outlyer@outlyer.net>
 *
 * License: http://www.gnu.org/copyleft/lgpl.html LGPL
 *
 * Extra DOM-like methods (Note that at the time of writing they can't be
 * bound to the Document prototype, so they are global functions)
 *
 *    getFirstChildByTagName(HTMLElement parent, String tagName)
 *       First child of parent with tag tagName, NOT-RECUSIVE
 *    getFirstSiblingByTagName(HTMLElement node, String tagName)
 *       See above
 *    getChildrenByClassName(Node parent, String className)
 *       Obtains an array with the children of parent that have
 *       the class className (supports multi-class elements), NOT-RECURSIVE
 *    getChildrenByTagName(Node parent, String tagName)
 *       Obtains an array with the children of parent that have
 *       the tag tagName, NOT-RECURSIVE
 *
 *    hasClass(HTMLElement element, String className)
 *       Checks if element has class className, supports multi-class elements
 *    removeClass(HTMLElement element, String className)
 *       Remove class className from element's classes if present, supports
 *       multi-class elements
 */
// boolean hasClass(HTMLElement e, String className)
function hasClass(e, c) {
	if (!document.getElementById) return false;
	if (!e.className) return false;
	return null!=e.className.match(new RegExp('^(w*|.* )'+c+'(w*| .*)$'));
}
// void removeClass(HTMLElement e, String className)
function removeClass(e, c) {
	if (!hasClass(e, c)) return;
	e.className = e.className.replace(new RegExp(c,'g'),'');
}
// HTMLElement getFirstChildByTagName(HTMLElement parent, String tagName)
function getFirstChildByTagName(e,tag){
	if (!document.getElementById) return null;
	var f=null;
	for (var i=0;i<e.childNodes.length;++i) {
		if (e.childNodes[i].tagName == tag) {
			f = e.childNodes[i];
			break;
		}
	}
	return f;
}
// HTMLElement getFirstSiblingByTagName(HTMLElement node, String tagName)
function getFirstSiblingByTagName(e,t) {
	if (!document.getElementById) return null;
	var f=null;
	if (null==e.nextSibling || e.nextSibling.tagName==t) return e.nextSibling;
	return getFirstSiblingByTagName(e.nextSibling,t);
}
// Array getChildrenByClassName(Node parent, String className)
function getChildrenByClassName(n,c) {
if (!document.getElementById) return null;
	var a=new Array();
	for (var i=0; i<n.childNodes.length; ++i) {
		var x=n.childNodes[i]
		if(hasClass(x,c)){a.push(x);}
		a.concat(getChildrenByClassName(x,c));
	}
	return a;
}
// Array getChildrenByTagName(Node parent, String tagName)
function getChildrenByTagName(n,t) {
	if (!document.getElementById) return null;
	var a=new Array();
	for(var i=0; i<n.childNodes.length;++i){
		var x=n.childNodes[i];
		if(x.tagName==t){a.push(x);}
		a.concat(getChildrenByTagName(x,t));
	}
}

Downloadable file: http://p.outlyer.net/graveyard/net_outlyer_dom_exts.js

Old code: JavaScript x-browser arrays (2005, 2010)

Last update: 2010-12-09

This block of code is deprecated, I'm only keeping it for archival purposes, see below for a more up-to-date equivalent

/*
 * Public Domain
 *
 * Created by Toni Corvera <outlyer@outlyer.net>, 2005
 *
 * Defines Array.find, and Array.merge
 */
// int Array.find(Object)
if (!Array.indexOf) {/*IE has no indexOf*/
	Array.prototype.find = function(what) {
		for (var i=0; i<this.length; ++i) {
			if (this[i]==what) {
				return i;
			}
		}
		return -1;
	}
}
else {
	Array.prototype.find=Array.prototype.indexOf;
}
// void Array.merge(Array)
Array.prototype.merge = function(a) {
	for (var i=0;i<a.length;++i) {
		this.push(a[i]);
	}
}

Old location: http://p.outlyer.net/graveyard/net_outlyer_arrays.js.

Array.find and Array.merge are equivalent to the standard methods Array.indexOf and Array.concat.

  • Array.concat was added to IE in version 4.0
  • Array.push was added on version 5.5.
  • Array.indexOf is not so widely supported (IE6 doesn't support it -I'm yet to try higher versions-, Chrome does)

Updated:

// int Array.indexOf(Object)
if (!Array.indexOf) { /*IE has no indexOf*/
	//This prototype is provided by the Mozilla foundation and
	//is distributed under the MIT license.
	//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
	Array.prototype.indexOf = function(elt /*, from*/) {
		var len = this.length;
	    var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
	    if (from < 0)
			from += len;
		for (; from < len; from++) {
			if (from in this && this[from] === elt)
		        return from;
		}
		return -1;
	};
}
if (!Array.push) {
	Array.prototype.push = function(e) {
		this[this.length] = e;
		return this.length;
	};
}

References:

Script: video identification (Bash)

This script displays mplayer's information/identification lines of a multimedia file. Nowadays I prefer to use an undocumented feature of vcs (see below)

#!/bin/bash

# This script displays mplayer's information/identification lines of a multimedia file
# Put in the public domain. <http://w.corvera.eu/2007/11/shell-vidid/>

vididf() {
        mplayer -identify -frames 0 -ao null -vo null "$1" 2>/dev/null | grep ID_ | sort
}

for F in "$@"; do vididf "$F" ; done

Example:

$ vidid file.avi

ID_AUDIO_BITRATE=160000
ID_AUDIO_BITRATE=160000
ID_AUDIO_CODEC=mp3
ID_AUDIO_FORMAT=85
ID_AUDIO_ID=1
ID_AUDIO_NCH=0
ID_AUDIO_NCH=2
ID_AUDIO_RATE=0
ID_AUDIO_RATE=48000
ID_DEMUXER=avi
ID_FILENAME=file.avi
ID_LENGTH=2569.52
ID_VIDEO_ASPECT=0.0000
ID_VIDEO_BITRATE=1223960
ID_VIDEO_CODEC=ffodivx
ID_VIDEO_FORMAT=XVID
ID_VIDEO_FPS=25.000
ID_VIDEO_HEIGHT=360
ID_VIDEO_ID=0
ID_VIDEO_WIDTH=640
VCS version:
$ vcs -Z idonly somefile.avi
[...]
=========== Mplayer Identification ===========
Length: 42:39.52
Video
    Codec: XVID (Xvid (MPEG-4))
    Dimensions: 704x396
    FPS: 23.976
    Aspect:
Audio
    Codec: 85 (MPEG Layer III (MP3))
    Channels: 2
==============================================

=========== FFmpeg Identification ===========
Length: 42:39.51
Video
    Codec: mpeg4 (MPEG-4)
    Dimensions: 704x396
    FPS: 23.98
    Aspect: 16/9
Audio
    Codec: mp3 (MPEG Layer III (MP3))
    Channels: 2
=============================================

=========== Combined Identification ===========
Length: 42:38.01
Video
    Codec: XVID (Xvid (MPEG-4))
    Dimensions: 704x396
    FPS: 23.976
    Aspect: 16/9 (1.7778)
Audio
    Codec: 85 (MPEG Layer III (MP3))
    Channels: 2
=============================================
Scroll to Top