Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Last active August 29, 2015 14:20
Show Gist options
  • Save Cartman0/5cf352b1c04e14671b96 to your computer and use it in GitHub Desktop.
Save Cartman0/5cf352b1c04e14671b96 to your computer and use it in GitHub Desktop.
JavaScript で GoogleFeedAPI を使用して、RSS の記事を取得
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GoogleFeedApiテスト</title>
</head>
<body>
<div id="codezine"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
//url, 取得したいエントリ数, 表示させるタグid名
getRssFeed("http://rss.rssad.jp/rss/codezine/new/20/index.xml", 5, "codezine" );
function getRssFeed(url, entries_num, id){
google.load("feeds", "1");
function initialize() {
getFeed();
function getFeed() {
/* キャッシュを最新にして取得 */
var feed = new google.feeds.Feed(url + "?" + (new Date()).getTime());
/* エントリの表示数の設定 */
feed.setNumEntries(entries_num);
feed.load(function(result) {
if (result.error) {
console.log("Failed Get Feed.");
console.log("error_code " + result.error.code + ": " + result.error.message);
} else {
var container = document.getElementById(id);
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
//divタグ作成
var div = document.createElement("div");
div.setAttribute("id", i);
//aタグの作成
var a = document.createElement("a");
a.setAttribute("href", entry.link);
a.appendChild(document.createTextNode(entry.title));
//結合
div.appendChild(a);
container.appendChild(div);
}
}
});
}
}
google.setOnLoadCallback(initialize);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment