Performa Widget Random Post

by dharlaferdana

HTML

<div id='random-post-container'>Memuat...</div>

CSS

body {
  margin:0;
  padding:50px;
  background-color:#eee;
  font:normal normal 11px/1.4 Arial,Sans-Serif;
  color:black;
}

#random-post-container {
  width:auto;
  margin-left:15px; // Style by: www.dhf.web.id/
}

#random-post-container ul {
  margin:7px 0 0;
  padding:0;
}

#random-post-container li {
  list-style:none;
  margin:0 0 2px;
  background-color:#e5e5e5;
  padding:0 7px 0 7px;
  line-height:24px;
  height:24px;
  overflow:hidden;
  border-bottom:1px dotted #ccc;
}

#random-post-container a {
  text-decoration:none;
}

#random-post-container a:hover {
  text-decoration:underline;
}

#random-post-container strong {
  font:normal bold 13px/1.4 Arial,Sans-Serif;
}

JavaScript

/* Performa Widget Random Post
 * By Dharla Ferdana
 * http://www.dhf.web.id
 * https://plus.google.com/113542517587930625517/posts
 */

var homePage = 'http://www.dhf.web.id',
    maxResults = 7,
    containerId = 'random-post-container';
// Function to generate random number limited from `min` to `max`
// Used to create a valid and safe random feed `start-index`
function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Function to shuffle arrays
// Used to randomize order of the generated JSON feed
function shuffleArray(arr) {
    var i = arr.length, j, temp;
    if (i === 0) return false;
    while (--i) {
        j = Math.floor(Math.random() * (i + 1));
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    return arr;
}
// Get a random start index
function createRandomPostsStartIndex(json) {
    var startIndex = getRandomInt(1, (json.feed.openSearch$totalResults.$t - maxResults));
    // console.log('Get the post feed start from ' + startIndex + ' until ' + (startIndex + maxResults));
    document.write('<scr' + 'ipt src="' + homePage + '/feeds/posts/summary?alt=json-in-script&orderby=updated&start-index=' + startIndex + '&max-results=' + maxResults + '&callback=randomPosts"></scr' + 'ipt>');
}
// Widget's main function
function randomPosts(json) {
    var link, ct = document.getElementById(containerId),
        entry = shuffleArray(json.feed.entry),
        skeleton = "<strong>Random Post</strong><ul>";
    for (var i = 0, len = entry.length; i < len; i++) {
        for (var j = 0, jen = entry[i].link.length; j < jen; j++) {
            link = (entry[i].link[j].rel == "alternate") ? entry[i].link[j].href : '#';
        }
        skeleton += '<li>&#10070; <a href="' + link + '">' + entry[i].title.$t + '</a></li>';
    }
    ct.innerHTML = skeleton + '</ul>';
}
document.write('<scr' + 'ipt src="' + homePage +...