jQuery RSS/ATOM feed parser plugin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

  1. /* jFeed : jQuery feed parser plugin
  2. * Copyright (C) 2007 Jean-François Hovinne - http://www.hovinne.com/
  3. * Dual licensed under the MIT (MIT-license.txt)
  4. * and GPL (GPL-license.txt) licenses.
  5. */
  6. jQuery.getFeed = function(options) {
  7. options = jQuery.extend({
  8. url: null,
  9. data: null,
  10. cache: true,
  11. success: null,
  12. failure: null,
  13. error: null,
  14. global: true
  15. }, options);
  16. if (options.url) {
  17. if (jQuery.isFunction(options.failure) && jQuery.type(options.error)==='null') {
  18. // Handle legacy failure option
  19. options.error = function(xhr, msg, e){
  20. options.failure(msg, e);
  21. }
  22. } else if (jQuery.type(options.failure) === jQuery.type(options.error) === 'null') {
  23. // Default error behavior if failure & error both unspecified
  24. options.error = function(xhr, msg, e){
  25. window.console&&console.log('getFeed failed to load feed', xhr, msg, e);
  26. }
  27. }
  28. return $.ajax({
  29. type: 'GET',
  30. url: options.url,
  31. data: options.data,
  32. cache: options.cache,
  33. dataType: (jQuery.browser.msie) ? "text" : "xml",
  34. success: function(xml) {
  35. var feed = new JFeed(xml);
  36. if (jQuery.isFunction(options.success)) options.success(feed);
  37. },
  38. error: options.error,
  39. global: options.global
  40. });
  41. }
  42. };
  43. function JFeed(xml) {
  44. if (xml) this.parse(xml);
  45. }
  46. ;
  47. JFeed.prototype = {
  48. type: '',
  49. version: '',
  50. title: '',
  51. link: '',
  52. description: '',
  53. parse: function(xml) {
  54. if (jQuery.browser.msie) {
  55. var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  56. xmlDoc.loadXML(xml);
  57. xml = xmlDoc;
  58. }
  59. if (jQuery('channel', xml).length == 1) {
  60. this.type = 'rss';
  61. var feedClass = new JRss(xml);
  62. } else if (jQuery('feed', xml).length == 1) {
  63. this.type = 'atom';
  64. var feedClass = new JAtom(xml);
  65. }
  66. if (feedClass) jQuery.extend(this, feedClass);
  67. }
  68. };