Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created November 29, 2011 09:48
Show Gist options
  • Save brianhsu/1404212 to your computer and use it in GitHub Desktop.
Save brianhsu/1404212 to your computer and use it in GitHub Desktop.
PicasaWeb Random Album Widget [JavaScript]
/****************************************************
* PicasaWeb 隨機相簿 JavaScript Widget
*
* 使用方式:
*
* 1. 引入此 JavaScript 檔案
* 2. 在要顯示的地方使用,記得將 [USERNAME] 的部份改成自己的 PicasaWeb 帳號
* <script src="http://picasaweb.google.com/data/feed/base/user/[USERNAME]?kind=album&access=public&alt=json-in-script&callback=showRandomAlbums"></script>
*
****************************************************/
/**
* 取得相簿網址
*/
function getAlbumURL(album) {
var albumURL = ""
for (var i = 0; i < album.link.length; i++) {
if (album.link[i].rel == 'alternate') {
albumURL = album.link[i].href
break
}
}
return albumURL;
}
/**
* 顯示相簿連結
*
* 每本相簿是一個 <ul> 標籤
*
*/
function showAlbum(album) {
var albumTitle = album.title.$t
var albumThumbnail = album.media$group.media$thumbnail[0].url
var albumURL = getAlbumURL(album)
document.write('<li class="albumItem">')
document.write(' <a class="albumLink" href="' + albumURL + '" title="' + albumTitle +'" >')
document.write(' <img src="' + albumThumbnail + '"/></a>')
document.write('</li>')
}
/**
* 隨機顯示相簿
*/
function showRandomAlbums(picasaJSON) {
var shuffledAlbum = shuffle(picasaJSON.feed.entry)
document.write('<ul class="albumList">')
for (var i = 0; i < shuffledAlbum.length; i++) {
var album = shuffledAlbum[i];
showAlbum(album)
}
document.write('</ul>')
}
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
shuffle = function(o){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment