Userscripts Top Scripts
By Brad Stewart
—
Last update Nov 28, 2007
—
Installed
9,980 times.
// ==UserScript==
// @name Userscripts Top Scripts
// @namespace http://bradmont.net
// @description Adds a "Most Popular Scripts" box to userscripts.org
// @include http://userscripts.org/
// @include http://userscripts.org/#
// @include http://userscripts.org/?page=*
// ==/UserScript==
// Original script copyright (c) 2007 Brad Stewart,
// contributions from Aleksandar DjuriÄ and LouCypher
// Released under the GNU General Public License version 2
// (see http://www.gnu.org/licenses/gpl.txt)
// TODO:
// 1. Store the currently displayed dataset with GM_setValue & retrieve when rendering
// 2. Perhaps show new scripts by installs/day rather than total.
// 3. Most popular scripts by installs/day over lifetime
var DATA_VERSION = 0.2;
GM_addStyle(
'/* Top N Nav */ \n' +
'#top_n_nav {margin: 0 0 8px 1px; font-size: .8em; text-align: center;}\n' +
'#top_n_nav a {margin-left: -1px; padding: 5px; border-bottom: none;}\n' +
'#top_n_nav a.current {color: #111; text-decoration: none;}\n' +
'/* Top N List */ \n' +
'#top_n_table {width: 100%; border-collapse: separate; margin-bottom: 1em;}\n' +
'#top_n_table th, #top_n_table td {font-size: .7em;}\n' +
'#top_n_table th {border-right: none; text-align: left; padding: 3px 6px;}\n' +
'#top_n_table th.meta {border-right: 1px solid #222;}\n' +
'#top_n_table td {color: #A2A2A2; padding: 7px 5px 6px 5px; vertical-align: top; text-align: right;' +
'border-top: none; border-right: none; border-bottom: 1px solid #DDD; border-left: 1px solid #DDD;}\n' +
'#top_n_table td a {line-height: 1.4em;}' +
'#top_n_table td.script {text-align: left; border-left: none; padding: 6px 11px 7px 3px;}' +
'#top_n_table td.meta {color: #111; text-align: right; padding: 7px 8px 6px 4px;' +
'border-right: 1px solid #DDD; background-color: #EEE;}\n' +
'#top_n_table td#extra {padding: 1px; border: none; font-size: .8em; text-align: left;}\n' +
'#top_n_table td#extra a {text-decoration: none; border: 1px solid #DDD; margin-left: -1px; padding: 2px 5px;}\n'
);
function updateData(){
GM_xmlhttpRequest({
method: 'GET',
url: 'http://bradmont.net/media/nums.txt',
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
},
onload: processData
});
}
function processData(responseDetails){
if (responseDetails.readyState == 4){
var datasets = ["TOP10", "YESTERDAY", "WEEK", "NEW"];
var data = responseDetails.responseText;
for (var i = 0; i < datasets.length; i++){
var field = datasets[i];
var top = data.indexOf("==" + field + "==") + 4 + field.length;
var bottom = data.indexOf("==/" + field + "==");
GM_setValue(field, data.substring(top, bottom));
}
d = new Date();
today = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "-" + DATA_VERSION;
GM_setValue("last_updated", today);
setup();
} else {
return;
}
}
function start(){
d = new Date();
today = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "-" + DATA_VERSION;
if (GM_getValue("last_updated") != today){
updateData();
} else {
setup();
}
}
function setup(){
rightDiv = document.getElementsByTagName("h6")[2].parentNode;
rightDiv.innerHTML="";
sibling = rightDiv.getElementsByTagName("form")[0];
heading = document.createElement("h6");
heading.appendChild(document.createTextNode("Top Scripts"));
rightDiv.appendChild(heading);
table = document.createElement("table");
table.setAttribute("id", "top_n_table");
table.setAttribute("cellspacing", "0");
rightDiv.appendChild(make_links_row());
rightDiv.appendChild(table);
table.appendChild(make_header_row());
dataSet = GM_getValue("dataSet", "NEW");
data = GM_getValue(dataSet, "[]");
set_data(eval(data));
}
function make_links_row(){
row = document.createElement("div");
row.setAttribute("id", "top_n_nav");
dataSet = GM_getValue("dataSet");
a = document.createElement("a");
row.appendChild(a);
a.setAttribute("href", "#");
a.setAttribute("id", "new");
if (dataSet == "NEW"){
a.setAttribute("class", "current");
}
a.addEventListener('click', show_new_downloads, true);
a.appendChild(document.createTextNode("New"));
a = document.createElement("a");
row.appendChild(a);
a.setAttribute("href", "#");
a.setAttribute("id", "yesterday");
if (dataSet == "YESTERDAY"){
a.setAttribute("class", "current");
}
a.addEventListener('click', show_yesterday_downloads, true);
a.appendChild(document.createTextNode("Day"));
a = document.createElement("a");
row.appendChild(a);
a.setAttribute("href", "#");
a.setAttribute("id", "week");
if (dataSet == "WEEK"){
a.setAttribute("class", "current");
}
a.addEventListener('click', show_weekly_downloads, true);
a.appendChild(document.createTextNode("Week"));
a = document.createElement("a");
a.setAttribute("href", "#");
a.setAttribute("id", "alltime");
if (dataSet == "TOP10"){
a.setAttribute("class", "current");
}
row.appendChild(a);
a.addEventListener('click', show_total_downloads, true);
a.appendChild(document.createTextNode("Ever"));
return row;
}
function make_header_row(){
row = document.createElement("tr");
cell = document.createElement("th");
row.appendChild(cell);
cell.appendChild(document.createTextNode("#"));
cell = document.createElement("th");
row.appendChild(cell);
cell.appendChild(document.createTextNode("Name"));
cell = document.createElement("th");
row.appendChild(cell);
cell.setAttribute("class", "meta");
cell.appendChild(document.createTextNode("Installs"));
return row;
}
function more_results(e){
e.preventDefault();
count = GM_getValue("num_results", 10);
if (count < 50){
GM_setValue("num_results", count + 10);
}
data = GM_getValue(GM_getValue("dataSet", "NEW"), "[]");
set_data(eval(data));
}
function less_results(e){
e.preventDefault();
count = GM_getValue("num_results", 10);
if (count >= 10){
GM_setValue("num_results", count - 10);
}
data = GM_getValue(GM_getValue("dataSet", "NEW"), "[]");
set_data(eval(data));
}
function show_total_downloads(e) {
e.preventDefault();
data = GM_getValue("TOP10", "[]");
GM_setValue("dataSet", "TOP10");
set_data(eval(data));
remove_previous_selection();
active = document.getElementById('alltime');
active.setAttribute("class","current");
}
function show_weekly_downloads(e) {
e.preventDefault();
GM_setValue("dataSet", "WEEK");
data = GM_getValue("WEEK", "[]");
set_data(eval(data));
remove_previous_selection();
active = document.getElementById('week');
active.setAttribute("class","current");
}
function show_yesterday_downloads(e) {
e.preventDefault();
GM_setValue("dataSet", "YESTERDAY");
data = GM_getValue("YESTERDAY", "[]");
set_data(eval(data));
remove_previous_selection();
active = document.getElementById('yesterday');
active.setAttribute("class","current");
}
function show_new_downloads(e) {
e.preventDefault();
GM_setValue("dataSet", "NEW");
data = GM_getValue("NEW", "[]");
set_data(eval(data));
remove_previous_selection();
active = document.getElementById('new');
active.setAttribute("class","current");
}
function remove_previous_selection(e) {
var topnav,links,i;
topnav = document.getElementById("top_n_nav");
links = topnav.getElementsByTagName('a');
for(i in links) {
if(links[i].className) {
links[i].removeAttribute("class");
}
}
}
function set_data(source) {
clear_top_table();
table = document.getElementById("top_n_table");
size = GM_getValue("num_results", 10);
if (size > source.length){
size = source.length;
}
for (i=0; i < size; i++){
row = document.createElement("tr");
table.appendChild(row);
firstcell = document.createElement("td");
firstcell.appendChild(document.createTextNode((i+1) + "."));
row.appendChild(firstcell);
cell = document.createElement("td");
cell.setAttribute("class", "script");
row.appendChild(cell);
a = document.createElement("a");
cell.appendChild(a)
a.setAttribute("href", "/scripts/show/" + source[i][0]);
a.appendChild(document.createTextNode(source[i][1]));
cell = document.createElement("td");
row.appendChild(cell);
cell.setAttribute("class", "meta");
cell.appendChild(document.createTextNode(source[i][2]));
}
row = document.createElement("tr");
table.appendChild(row);
cell = document.createElement("td");
row.appendChild(cell);
cell.setAttribute("colspan", "3");
cell.setAttribute("id", "extra");
a = document.createElement("a");
cell.appendChild(a)
a.setAttribute("href", "#");
a.appendChild(document.createTextNode("+"));
a.setAttribute("title", "show more scripts");
a.addEventListener("click", more_results, true);
a = document.createElement("a");
cell.appendChild(a)
a.setAttribute("href", "#");
a.setAttribute("title", "show less scripts");
a.appendChild(document.createTextNode("-"));
a.addEventListener("click", less_results, true);
}
function clear_top_table() {
table = document.getElementById("top_n_table");
rows = table.getElementsByTagName("tr");
for (i = rows.length-1; i > 0; i-- ){
rows[i].parentNode.removeChild(rows[i]);
}
}
start();