Skip to content

Instantly share code, notes, and snippets.

@djyde
Last active January 3, 2016 19:29
Show Gist options
  • Save djyde/8508659 to your computer and use it in GitHub Desktop.
Save djyde/8508659 to your computer and use it in GitHub Desktop.
Parse RSS only in Javascript
//It depends on Google Feed API
//Check <script type="text/javascript" src="https://www.google.com/jsapi"></script> is included in HTML
var ggrss = (function(){
var cache = [];
return {
parse: function(url,num,callback){
google.load("feeds","1");
//I found that google.load() should not be called while your page is ready, or the page will be cleared
//So the correct way to load API is loading it before all the DOM loaded
function initialize() {
var feed = new google.feeds.Feed(url);
feed.setNumEntries(num);
feed.load(function(result) {
if (!result.error) {
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
cache.push(entry)
}
callback(result.error,cache)
}
});
}
google.setOnLoadCallback(initialize);
},
}
}())
@djyde
Copy link
Author

djyde commented Jan 19, 2014

Example

ggrss.parse("http://36kr.com/feed",10,function(err,result){
  if(!err){
    for(var i = 0; i < result.length; i++){
      console.log(result[i].title);
      console.log(result[i].content);
      console.log(result[i].link);
    }
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment