Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds a table showing how many comments each kid has added to each post.
// ==UserScript==
// @name Add Post Table to Kid Blog
// @namespace http://christian.gen.co/
// @version 0.1
// @description Adds a table showing how many comments each kid has added to each post.
// @author Christian Genco
// @match http://kidblog.org/*/wp-admin/users.php
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
function getCommentData(html){
return $(html).find('tr.comment').map(function(i,e){
$e = $(e);
submittedOn = $e.find('.submitted-on a').first().text();
// console.log(submittedOn);
url = $e.find('.submitted-on a').first().attr('href');
level = 1;
if($e.find('.submitted-on').text().indexOf('In reply to') !== -1){
level = 2;
}
orig = $e.find(".kb-original-post").text();
postTitle = orig.substring(0,orig.indexOf('Edit'));
postUrl = $e.find(".kb-original-post > a").last().attr('href');
author = $e.find(".author strong").text().trim();
return {
submittedOn: submittedOn,
url: url,
level: level,
postTitle: postTitle,
postUrl: postUrl,
author: author
}
})
}
function refreshPosts(comments){
posts = {};
$.each(comments, function(i,c){
posts[c.postUrl] || (posts[c.postUrl] = {});
posts[c.postUrl].title = c.postTitle;
posts[c.postUrl].authors || (posts[c.postUrl].authors = {});
posts[c.postUrl].authors[c.author] || (posts[c.postUrl].authors[c.author] = {'1': 0, '2': 0});
posts[c.postUrl].authors[c.author][''+c.level]++;
});
return posts;
}
function getAuthors(comments){
authors = [];
$.each(comments, function(i,c){
authors.push(c.author);
});
return $.unique(authors).sort();
}
function refreshTable(comments){
var posts = refreshPosts(comments);
var authors = getAuthors(comments);
console.dir(posts);
var tbl = document.createElement('table');
tbl.setAttribute('id', 'postTable');
tbl.style.width='100%';
tbl.style.border = "1px solid black";
// headers
var tr = tbl.insertRow();
var td = tr.insertCell();
// td.appendChild(document.createTextNode(post.title));
for(var url in posts){
post = posts[url];
var td = tr.insertCell();
td.setAttribute('colSpan','2');
var a = document.createElement('a');
a.appendChild(document.createTextNode(post.title));
a.href = url;
td.appendChild(a);
}
for(var i in authors){
var author = authors[i];
var tr = tbl.insertRow();
if(i%2===0) tr.style.backgroundColor = "#ccc";
var td = tr.insertCell();
td.appendChild(document.createTextNode(author));
for(var url in posts){
post = posts[url];
for(k=1; k<=2;k++){
var td = tr.insertCell();
var n = post.authors[author] ? post.authors[author][k] : '';
if(+n===0) n="";
td.appendChild(document.createTextNode(n));
}
// td.style.border = "1px solid black";
}
}
$("#postTable").remove();
$(tbl).insertAfter('.subsubsub');
}
// get list of urls
var students = $('a.edit').map(function(i,e){return $(e).attr('href')})
var comments = [];
students.each(function(i,path){
url = "http://kidblog.org/RJLGreen3/wp-admin/" + path;
$.get(url, function(html){
// console.dir(getCommentData(html));
comments.push.apply(comments, getCommentData(html));
refreshTable(comments);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.