Advertisement
mspotilas

Customizable Author Box with post count, with url override

Jan 2nd, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <style type="text/css">
  2. .author-line     {margin: 3px 0;}
  3. .author-avatar   {vertical-align:middle;}
  4. </style>
  5. <script type="text/javascript">
  6. //
  7. // Blog authors gadget, works like multi author profile gadget,
  8. // but with post counts and avatars, ordered most written first,
  9. // by MS-potilas 2013. See http://yabtb.blogspot.fi/2012/06/profile-gadget-with-avatars-and-post.html
  10. // Special version which allows overriding of author profile urls.
  11. //
  12. var auth_url = {};
  13. //
  14. // CONFIG:
  15. var maxUserNameLength = 42; // 0: don't cut, >4: cut usernames
  16. //
  17. var txtAuthorLine = '[image] [user] has written [count] [post]'; // can also use [#] (=position)
  18. //
  19. var sizeAvatar = 20;
  20. var cropAvatar = true;
  21. //
  22. var urlNoAvatar = "http://lh4.googleusercontent.com/-069mnq7DV_g/TvgRrBI_JaI/AAAAAAAAAic/Iot55vywnYw/s"+sizeAvatar+"/avatar_blue_m_96.png"; // http://www.blogger.com/img/avatar_blue_m_96.png resizeable
  23.  
  24. // Override author links, in case they are missing from the feed:
  25. auth_url['Izmail'] = 'http://www.somelink.com/';
  26. auth_url['Macmohan'] = 'http://www.someotherlink.com/';
  27.  
  28. // config end
  29.  
  30. function replaceAuthorVars(text, item, position)
  31. {
  32.   if(!item || !item.author) return text;
  33.   var author = item.author;
  34.  
  35.   var authorUri = "";
  36.   if(author.uri && author.uri.$t != "")
  37.     authorUri = author.uri.$t;
  38.  
  39.   var avaimg = urlNoAvatar;
  40.   if(author.gd$image && author.gd$image.src)
  41.     avaimg = author.gd$image.src;
  42.   if(avaimg == "http://img2.blogblog.com/img/b16-rounded.gif" && urlNoAvatar != "")
  43.     avaimg = urlNoAvatar;
  44.   var newsize="s"+sizeAvatar;
  45.   avaimg = avaimg.replace(/\/s\d\d+-c\//, "/"+newsize+"-c/");
  46.   if(cropAvatar) newsize+="-c";
  47.   avaimg = avaimg.replace(/\/s\d\d+(-c){0,1}\//, "/"+newsize+"/");
  48.  
  49.   var authorName = author.name.$t;
  50.   if(auth_url[authorName] && auth_url[authorName] != "")
  51.     authorUri = auth_url[authorName];
  52.   var imgcode = '<img class="author-avatar" height="'+sizeAvatar+'" width="'+sizeAvatar+'" title="'+authorName+'" src="'+avaimg+'" />';
  53.   if(authorUri!="") imgcode = '<a href="'+authorUri+'">'+imgcode+'</a>';
  54.  
  55.   if(maxUserNameLength > 3 && authorName.length > maxUserNameLength)
  56.     authorName = authorName.substr(0, maxUserNameLength-3) + "...";
  57.   var authorcode = authorName;
  58.   if(authorUri!="") authorcode = '<a class="profile-name-link" href="'+authorUri+'">'+authorcode+'</a>';
  59.  
  60.   text = text.replace('[user]', authorcode);
  61.   text = text.replace('[image]', imgcode);
  62.   text = text.replace('[#]', position);
  63.   text = text.replace('[count]', item.count);
  64.   if(item.count != 1)
  65.     text = text.replace('[post]', "posts");
  66.   else
  67.     text = text.replace('[post]', "post");
  68.   return text;
  69. }
  70.  
  71. var blauthors = {};
  72. var blndxbase = 1;
  73. function showAuthors(json) {
  74.   for(var i = 0 ; i < json.feed.entry.length ; i++ ) {
  75.     var entry = json.feed.entry[i];
  76.     var authorUri = "";
  77.     if(entry.author[0].uri && entry.author[0].uri.$t != "")
  78.       authorUri = entry.author[0].uri.$t;
  79.  
  80.     var authorName = entry.author[0].name.$t;
  81.  
  82.     if(blauthors[authorName])
  83.       blauthors[authorName].count++;
  84.     else {
  85.       var aut = new Object();
  86.       aut.author = entry.author[0];
  87.       aut.count = 1;
  88.       blauthors[authorName] = aut;
  89.     }
  90.   }
  91.   if(json.feed.entry.length == 500) {
  92.     blndxbase += 500;
  93.     document.write('<script type="text/javascript" src="http://'+window.location.hostname+'/feeds/posts/default?redirect=false&max-results=500&start-index='+blndxbase+'&alt=json-in-script&callback=showAuthors"></'+'script>');
  94.     return;
  95.   }
  96.   var tuplear = [];
  97.   for(var key in blauthors) tuplear.push([key, blauthors[key]]);
  98.  
  99.   tuplear.sort(function(a, b) {
  100.     if(b[1].count-a[1].count)
  101.         return b[1].count-a[1].count;
  102.     return (a[1].author.name.$t.toLowerCase() < b[1].author.name.$t.toLowerCase()) ? -1 : 1;
  103.   });
  104.  
  105.   // output authors:
  106.   document.write('<di'+'v class="blog-author">');
  107.   for(var i = 0; i < tuplear.length ; i++) {
  108.     var item = tuplear[i][1];
  109.     document.write('<di'+'v class="author-line">');
  110.     document.write(replaceAuthorVars(txtAuthorLine, item, i+1));
  111.     document.write('</d'+'iv>');
  112.   }
  113.   document.write('</d'+'iv>');
  114. }  
  115. document.write('<script type="text/javascript" src="http://'+window.location.hostname+'/feeds/posts/default?redirect=false&max-results=500&alt=json-in-script&callback=showAuthors"></'+'script>');
  116. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement