Warning

BRPS is dead. (2015-12-02T02:44:34Z)

After 40 posts, I decided to add my own BRPS1 (Blogger Related Posts Service) gadget to this blog. I modified the current brps.js and embedded it into source, it uses jQuery 1.3 and it would cause some problem in this blog.

Here is the code:

<div id='related_posts'></div>
<script>
// GPL'ed, verson 3 or later
function BRPS_watchdog() {
  if (window.brps_start == undefined)
    return
  diff = (new Date()).valueOf() - brps_start;
  if (diff >= 30 * 1000) {
    $('#related_posts').empty();
    $('#related_posts').append('<p style="color:#f00">Something went wrong with BRPS server!</p>');
    window.brps_start = undefined;
    }
  else
    window.setTimeout('BRPS_watchdog()', 5000);
  }

function BRPS_get() {
  var key = '%%%%% YOUR BRPS KEY %%%%%';
  var blog_id = '%%%%% YOUR BLOGGER BLOG ID %%%%%';

  // Get Post ID
  var links = $("link[rel='alternate']");
  var post_id = '';
  for (var i=0; i<links.length; i++) {
    m = /.*\/feeds\/(\d+)\/comments\/default/.exec($(links[i]).attr('href'));
    if (m != null && m.length == 2) {
      post_id = m[1];
      break;
      };
    }
  var $rps = $('#related_posts');
  if (blog_id != '' && post_id != '') {
    if (window.brps_start == undefined)
      window.setTimeout('BRPS_watchdog()', 5000);
    window.brps_start = (new Date()).valueOf();
    $rps.empty();
    $('<i>Loading...</i>').appendTo($rps);
    $.getJSON("http://brps.appspot.com/get?blog=" + blog_id + "&post=" + post_id + "&key=" + key + "&callback=?",
        function(data){
          window.brps_start = undefined;
          $rps.empty();
          if (data.error) {
            $('<p>' + data.error + '</p>').appendTo($rps);
            if (data.code == 3)
              // Need to retry in 5 seconds
              window.setTimeout('BRPS_get()', 5000);
            }
          else {
            if (data.entry.length > 0) {
              var $rps_ul = $('<ul></ul>').appendTo($rps).hide();
              $.each(data.entry, function(i, entry){
                $('<li/>')
                  .append($('<a/>').attr('href', entry.link).attr('title', 'Score: ' + entry.score.toString()).text(entry.title))
                  .appendTo($rps_ul)
                  ;
                });
              $rps_ul.slideDown('slow').fadeIn('slow');
              }
            else {
              $('<p>No related posts found.</p>').appendTo($rps);
              }
            }
        });
    }
  else {
    $('<p>Only available in single post.</p>').appendTo($rps);
    }
  }
$(BRPS_get);
</script>

Since my blog always requires jQuery, therefore I didnt put the embedding code in the code above. You might need the following code to be before the code above:

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>

A few changes:

  • The Blog ID2 and Key1 are hand coded in script.
  • Remove the BRPS options, because I know what I want.
  • No longer rendering the title and if the page is not a single post page, then it will show a message instead of emptying gadget to show nothing.
  • The list will be sliding down and fading in.

Generally, it should run faster, though you wouldnt feel that.


[1](1, 2) The brps.js is no longer available to new users, see Using BRPS new method to get related posts list.
[2]Search your blog HTML source for blogID, you will see it.