Sort AMO 'Add to a collection' Menu

By Erik Vold Last update Oct 14, 2009 — Installed 242 times.

There are 1 previous version of this script.

// ==UserScript==
// @name			Sort AMO 'Add to a collection' Menu
// @author			Erik Vold
// @namespace		amoSortAddToCollectionMenu
// @include			http://addons.mozilla.org/*
// @include			https://addons.mozilla.org/*
// @version			0.1
// @license			GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @datecreated		2009-10-13
// @lastupdated		2009-10-14
// @description		This userscript will sort the AMO 'Add to a collection' menu.
// ==/UserScript==

var amoSortAddToCollectionMenu = {};
amoSortAddToCollectionMenu.init = function() {
	var colsDD = document.getElementById('publish_to');
	if(!colsDD) return;
	var optionsAry = colsDD.options, i, j, optionsValArySorted, optionsValAry = [];
	// skip first and last which is not a collection
	for( i=1; i<optionsAry.length-1; i++ ) {
		optionsValAry.push( optionsAry[i].innerHTML );
	}
	// trim function
	var trimFunc = function(input){
		return input.toLowerCase().replace( /^\s*(the|a)\s+/i , '' );
	}
	// copy and sort and save the copied array
	optionsValArySorted = optionsValAry.slice(0).map( trimFunc ).sort();
	for ( i=0; i<optionsValArySorted.length; i++ ) {
		for ( j=i+1; j<optionsAry.length-1; j++ ) {
			if ( trimFunc(optionsAry[j].innerHTML)==optionsValArySorted[i] ) {
				if ( (i+1) == j ) break;
				optionsAry[j].parentNode.insertBefore( optionsAry[j], optionsAry[i+1] );
				break;
			}
		}
	}
	return;
}
amoSortAddToCollectionMenu.init();