By Brad Stewart
—
Last update
Mar 15, 2007
—
Installed
3,729 times.
// User Scripts Search Sorting, v 0.3.1, 2007-March-14
// Copyright (c) Brad Stewart, 2007
// Released under the GNU GPL, v2
// ==UserScript==
// @name User Scripts Search Sorting
// @namespace http://bradmont.net
// @description Sorts search results on userscripts.org
// @include http://userscripts.org/scripts/search*
// @include http://*.userscripts.org/scripts/search*
// @include http://userscripts.org/tag/*
// @include http://*.userscripts.org/tag/*
// @include http://userscripts.org/users/*scripts
// @include http://*.userscripts.org/users/*scripts
// ==/UserScript==
function add_headers(){
var headerRow = document.getElementsByTagName("table")[0].getElementsByTagName("tr")[0];
var info = headerRow.getElementsByTagName("th")[0];
info.appendChild(document.createTextNode(" "));
var link = document.createElement("a");
link.appendChild(document.createTextNode("Name"));
link.setAttribute("href", "#");
link.addEventListener('click', sort_by_name, true);
info.appendChild(link);
var info = headerRow.getElementsByTagName("th")[1];
info.appendChild(document.createTextNode(" "));
var link = document.createElement("a");
link.appendChild(document.createTextNode("Installs"));
link.setAttribute("href", "#");
link.addEventListener('click', sort_by_installs, true);
info.appendChild(link);
info.appendChild(document.createTextNode(" "));
var link = document.createElement("a");
link.appendChild(document.createTextNode("Comments"));
link.setAttribute("href", "#");
link.addEventListener('click', sort_by_comments, true);
info.appendChild(link);
info.appendChild(document.createTextNode(" "));
var link = document.createElement("a");
link.appendChild(document.createTextNode("Date"));
link.setAttribute("href", "#");
link.addEventListener('click', sort_by_date, true);
info.appendChild(link);
}
function sort_by_date(){
sort_scripts(3);
}
function sort_by_comments(){
sort_scripts(1);
}
function sort_by_installs(){
sort_scripts(2);
}
function sort_by_name(){
sort_scripts(0);
}
function sort_scripts(sort_field){
// there's only one table on the page, so...
var scriptsTable = document.getElementsByTagName("table")[0];
var rows = scriptsTable.getElementsByTagName("tr");
var headerRow = rows[0]; // we don't want to sort the headers.
var scripts = new Array();
var parent=headerRow.parentNode; // for removing & adding child nodes
while (rows.length > 1){
script = rows[1];
script=script.parentNode.removeChild(script);
// the 3rd paragraph in the <tr> is the one that contains the install count
if (sort_field == 1 || sort_field == 2){
metric = script.getElementsByTagName("p")[sort_field].wrappedJSObject.firstChild.data;
metric = parseInt(metric.split(" ")[0].replace(",", ""));
} else if (sort_field == 3){
metric = script.getElementsByTagName("abbr")[0].getAttribute("title");
metric=metric.replace(/[T:Z]/g, "-");
parts = metric.split("-");
metric = new Date(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], 0).getTime();
} else if (sort_field == 0){
// name
metric = script.getElementsByTagName("a")[1].wrappedJSObject.firstChild.data.toLowerCase();
}
scripts.push([metric, script.wrappedJSObject]);
}
if (sort_field != 0){
scripts.sort(
function(a, b) { return b[0]-a[0];} // descending sort
);
} else {
scripts.sort();
}
for (var i = 0; i < scripts.length; i++){
script = scripts[i];
parent.appendChild(script[1]);
}
}
add_headers();
sort_scripts(2);