Skip to content

Instantly share code, notes, and snippets.

@toshi2220
Last active Dec 31, 2015
Embed
What would you like to do?
<!-- 参考アドレス:http://chiroru.hatenablog.com/entry/2013/10/25/213517 -->
<!-- HTML : new_entries -->
<div class="hatena-module-title">
<a href="/archive">Recent entries</a>
</div>
<!-- 新しい記事を表示する -->
<div id="new_entries_feed" align="center">Now Loading...</div>
<!-- SCRIPT : new_entries_feed -->
<script src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("feeds", "1");
function initialize() {
//RSSフィードの取得
var blogURL = "ブログのURL";
var feed = new google.feeds.Feed( blogURL+ "rss");
//取得するフィード数
feed.setNumEntries(5);
//実際に読む込む
feed.load(function(result) {
//読み込めたか判別
if (!result.error) {
//表示部分を選択
var container = document.getElementById("new_entries_feed");
//変数の初期化
var useFeed = "";
//Feedの処理
for (var i = 0; i < result.feed.entries.length; i++) {
//Feedを一つ抽出
var entry = result.feed.entries[i];
//日付を抽出
var pdate = new Date(entry.publishedDate);
var strdate = (pdate.getMonth() + 1) + '月' + pdate.getDate() + '日';
//画像がない場合を考慮
var no_image = '代わりの画像のURL';
//最初の画像を抽出
var first_image = (entry.content.match(/(http:){1}[\S_-]+\.(?:jpg|gif|png)/) != null) ? entry.content.match(/(http:){1}[\S_-]+\.(?:jpg|gif|png)/) : no_image.match(/(http:){1}[\S_-]+\.(?:jpg|gif|png)/);
//リストに突っ込む
useFeed += '<div class="htbl_new_entry"><a href="' + entry.link + '"><img class="htbl_new_entry_img" src="' + first_image[0] + '"><div class="htbl_new_entry_text"><span class="entry_title">' + entry.title + '</span><span class="entry_date">' + strdate + '</span></div></a></div>';
}
//リストを表示させる
container.innerHTML = '<div class="htbl_new_entries">' + useFeed + '</div>';
}
});
}
google.setOnLoadCallback(initialize);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment