滋此,带上个我的脚本(批量列出好友PP/Rank)求dalao帮忙优化。。写的又臭又长,想听听dalao的思路(主要是当时脑抽没写上异步也没啥思路,然后就经常卡。。然后好像是感觉有一些步骤多余了
if ($('.paddingboth').length > 5) {
domfriends=$('.paddingboth');
onlinefriends=domfriends[domfriends.length-2].children;
offlinefriends=domfriends[domfriends.length-1].children;
friends=[];
for (let i=0;i<onlinefriends.length;i++) {
if (onlinefriends[i].innerText !== 'No friends online') {
friends.push(onlinefriends[i].innerText);
}
}
for (let i=0;i<offlinefriends.length;i++) {
friends.push(offlinefriends[i].innerText);
}
document.body.innerText='';
document.write('已获取全部好友列表!<br>');
userdata={};
for (let i=0;i<friends.length;i++) {
$.ajax({
url:'https://osu.ppy.sh/u/'+friends[i],
async:false,
success:function(data) {
data=data.match(/var userId = (\d*)/)[1];
userdata[friends[i]]='-';
if (data !== null) {
userdata[friends[i]]=data;
}
}
});
}
document.write('已获取全部好友的osu! ID<br>');
userstdrank={};
usertaikorank={};
userctbrank={};
usermaniarank={};
idstart=0;
idend=friends.length;
document.write('共 '+idend+' 个好友<br>');
function getrank(mode,uid,uname) {
$.ajax({
url:'https://osu.ppy.sh/pages/include/profile-general.php?u='+uid+'&m='+mode,
async:false,
success:function(data) {
data=data.match(/Performance<\/a>: (.*?)<\/b>/);
if (data === null) {
data=[];
data[1]='-';
}
switch (mode) {
case 0:
userstdrank[uname]=data[1];
break;
case 1:
usertaikorank[uname]=data[1];
break;
case 2:
userctbrank[uname]=data[1];
break;
case 3:
usermaniarank[uname]=data[1];
break;
}
}
});
}
function writerank() {
if (idstart < idend) {
idstart++;
getrank(0,userdata[friends[idstart]],friends[idstart]);
getrank(1,userdata[friends[idstart]],friends[idstart]);
getrank(2,userdata[friends[idstart]],friends[idstart]);
getrank(3,userdata[friends[idstart]],friends[idstart]);
document.write(friends[idstart]+' Rank:STD '+userstdrank[friends[idstart]]+' Taiko '+usertaikorank[friends[idstart]]+' CTB '+userctbrank[friends[idstart]]+' Mania '+usermaniarank[friends[idstart]]+'<br>');
} else {
clearInterval(iid);
$('body').css('background-color','#009ACD');
}
}
iid=setInterval(writerank,300);
}