Skip to content

Instantly share code, notes, and snippets.

@urakey
Created April 9, 2014 08:37
Show Gist options
  • Save urakey/10242196 to your computer and use it in GitHub Desktop.
Save urakey/10242196 to your computer and use it in GitHub Desktop.
A Pen by akey.

API: Hatena Bookmark

特定サイトのはてなブックマーク人気エントリーを取得して表示する

A Pen by akey on CodePen.

License.

<ul id="hatenaEntries"></ul>
(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);
@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