Skip to content

Instantly share code, notes, and snippets.

@dolpen
Created September 11, 2010 18:27
Show Gist options
  • Save dolpen/575422 to your computer and use it in GitHub Desktop.
Save dolpen/575422 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name lists 2 add
// @namespace www.dolpen.net
// @description リストのレコメンドをします
// @include http://twitter.com/*
// @require http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js
// ==/UserScript==
//prototype
var ListTagNode=function(str){
this.nodeName=str||'';
this.weight=(str&&str!='')?1:0;
this.score=0;
this.distance=0;
this.childs={};
};
ListTagNode.prototype.setDistance=function(d){
this.distance=d;
}
ListTagNode.prototype.getRanking=function(){
return this.calc().slice(0,-1).sort(
function(a,b){
if(a.score!=b.score)return b.score-a.score;
return b.nodeName.length-a.nodeName.length;
}
);
};
ListTagNode.prototype.toString=function(){
var r=[];
for(var i in this.childs)r.push(this.childs[i].toString());
return this.nodeName+'='+this.score+':{\n'+r.join(',\n')+'\n}';
};
ListTagNode.prototype.calc=function(){
var r=[];
this.score=this.weight;
for(var i in this.childs){
r=r.concat(this.childs[i].calc());
this.score+=this.childs[i].score*this.distance;
}
r.push(this);
return r;
};
ListTagNode.prototype.addEntry=function(str){
if(this.nodeName==str){this.weight++;return;}
if(this.childs.hasOwnProperty(str)){this.childs[str].addEntry(str);return;}
for(var i in this.childs){
if(i.indexOf(str,0)==0){
this.childs[str] = new ListTagNode(str);
this.childs[str].setDistance(this.distance);
this.childs[str].childs.i=this.childs[i];
delete this.childs[i];
return;
}else if(str.indexOf(i,0)==0){
this.childs[i].addEntry(str);
return;
}
}
this.childs[str]=new ListTagNode(str);
this.childs[str].setDistance(this.distance);
};
//script
var list=function(){
$.ajax({
url:'/'+user+'/lists/memberships'+'?'+query,
async:true,
success: function(obj){
query=$(obj).find('#more').attr('href').split('?')[1];
$(obj).find('.list-info a').each(function(){
tree.addEntry(($(this).attr('href')).split('/')[2].toLowerCase());
});
var rank=tree.getRanking().slice(0,5),r='';
for(var i=0;i<rank.length;i++)r+='<br>'+rank[i].nodeName+'('+rank[i].score.toFixed(2)+')';
welem.empty().append($(r+'<br>'));
if(--cnt>0)setTimeout(list,1000)
}
});
};
var user;
var cnt=3;
var welem;
var tree;
var query='';
(function(){
if($('#profile-image').attr('src')==undefined)return;
welem=$('<span></span>');
$('#bio').parent().append(
$('<li></li>').append($('<span class="label">タグ</span>')).append(welem)
);
user=location.href.split('/')[3];
tree=new ListTagNode();tree.setDistance(0.3);
list();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment