特定サイトのはてなブックマーク人気エントリーを取得して表示する
Created
April 9, 2014 08:37
-
-
Save urakey/10242196 to your computer and use it in GitHub Desktop.
A Pen by akey.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul id="hatenaEntries"></ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) | |
{ | |
"use strict"; | |
/** | |
* @class Hatena | |
*/ | |
function Hatena(url, sort, $dom, tag) { | |
this.url = url; | |
this.sort = sort; | |
this.$dom = $dom; | |
this.tag = tag || 'p'; | |
this.init.apply(this); | |
} | |
Hatena.prototype = { | |
init: function() { | |
this.loadEntries(); | |
}, | |
loadEntries: function() { | |
var _this = this; | |
var d = $.Deferred(); | |
$.ajax({ | |
type: 'GET', | |
url: 'http://b.hatena.ne.jp/entrylist/json', | |
data: { | |
sort: _this.sort, | |
url: _this.url | |
}, | |
dataType: 'jsonp', | |
jsonp : 'callback', | |
success: function(data) { | |
_this.appendHtml(_this.$dom, _this.setDataForEachEntries(data)); | |
d.resolve(); | |
}, | |
error: function(e) { | |
console.log(e); | |
} | |
}); | |
return d.promise(); | |
}, | |
setDataForEachEntries: function(entries) { | |
var _this = this; | |
var htmlTag = _this.tag; | |
var title, link, count, htmlSrc; | |
var newCommers = []; | |
if(entries.length <= 0) return; | |
$.each(entries, function(index, entry){ | |
title = entry.title; | |
link = entry.link; | |
count = entry.count; | |
htmlSrc = '<' + htmlTag + '>'; | |
htmlSrc += '<a href="' + link + '">'; | |
htmlSrc += title; | |
htmlSrc += '</a>'; | |
htmlSrc += ' [' + count + '] '; | |
htmlSrc += '</' + htmlTag + '>'; | |
newCommers.push(htmlSrc); | |
}); | |
return newCommers; | |
}, | |
appendHtml: function($dom, htmlSrcArray) { | |
if(htmlSrcArray.length <= 0) return; | |
$.each(htmlSrcArray, function(index, htmlSrc){ | |
$dom.append(htmlSrc); | |
}); | |
} | |
}; | |
//=========================================================================== | |
// 処理 | |
//=========================================================================== | |
$(document).on({ | |
'ready': function(){ | |
var hatena = new Hatena('http://log.chocolateboard.net', 'count', $('#hatenaEntries'), 'li'); | |
} | |
}); | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import "compass/css3"; | |
body { | |
font-size: 85%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment