Last active
January 1, 2016 08:49
-
-
Save fddcddhdd/8120399 to your computer and use it in GitHub Desktop.
Google Ajax Feed APIを使ってみた(RSS表示するAPI)
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
<html> | |
<head> | |
<script type="text/javascript" src="https://www.google.com/jsapi"></script> | |
<script type="text/javascript"> | |
//RSSソース | |
const RSS_URL = "http://feeds.feedburner.com/messiahworks/rss"; | |
//取得するRSS記事数 | |
const RSS_NUM = 10; | |
//Google APIをロード。第一引数はAPIの種類(maps, search, feeds) 第二引数はAPIバージョン | |
google.load("feeds", "1"); | |
function initialize() { | |
//フィードオブジェクトを生成 | |
var feed = new google.feeds.Feed(RSS_URL); | |
//取得するRSS記事数を設定 | |
feed.setNumEntries(RSS_NUM); | |
// 指定されたURLからデータを持ってくる | |
feed.load(function(result) { | |
//出力先要素(ID指定)を変数に格納 | |
var container = document.getElementById("feed"); | |
if (!result.error){ | |
var htmlstr = ""; | |
// RSS記事の分だけループ | |
for (var i = 0; i < result.feed.entries.length; i++) { | |
//各記事を変数に格納 | |
var entry = result.feed.entries[i]; | |
//曜日の配列オブジェクトを生成 | |
myDay = new Array( "日","月","火","水","木","金","土" ); | |
//日付型変数に変換 | |
var pdate = new Date(entry.publishedDate); | |
// 好みの文字列へ加工 | |
var strdate = pdate.getFullYear() + '年' + (pdate.getMonth() + 1) + '月' + pdate.getDate() + '日(' | |
+ myDay[pdate.getDay()] + ')' + pdate.getHours() + '時' + pdate.getMinutes() + '分'; | |
//日付を表示 | |
htmlstr += '' + strdate + '<br>'; | |
//タイトルとリンクを表示 | |
htmlstr += '<b><a href="' + entry.link + '" target="_blank">' + entry.title + '</a></b><br><br>'; | |
} | |
// 要素に出力 | |
container.innerHTML = htmlstr; | |
}else{ | |
//エラー時は、エラーコードとエラーメッセージを表示する | |
container.innerHTML = result.error.code + ":" + result.error.message; | |
} | |
}); | |
} | |
//ブラウザに読み込まれたら、引数の関数(initialize)を実行する | |
google.setOnLoadCallback(initialize); | |
</script> | |
</head> | |
<body> | |
<div id="feed"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment