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.

170 lines
4.6 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. };
  69. function JFeedItem() {};
  70. JFeedItem.prototype = {
  71. title: '',
  72. link: '',
  73. description: '',
  74. updated: '',
  75. id: ''
  76. };
  77. function JAtom(xml) {
  78. this._parse(xml);
  79. };
  80. JAtom.prototype = {
  81. _parse: function(xml) {
  82. var channel = jQuery('feed', xml).eq(0);
  83. this.version = '1.0';
  84. this.title = jQuery(channel).find('title:first').text();
  85. this.link = jQuery(channel).find('link:first').attr('href');
  86. this.description = jQuery(channel).find('subtitle:first').text();
  87. this.language = jQuery(channel).attr('xml:lang');
  88. this.updated = jQuery(channel).find('updated:first').text();
  89. this.items = new Array();
  90. var feed = this;
  91. jQuery('entry', xml).each( function() {
  92. var item = new JFeedItem();
  93. item.title = jQuery(this).find('title').eq(0).text();
  94. item.link = jQuery(this).find('link').eq(0).attr('href');
  95. item.description = jQuery(this).find('content').eq(0).text();
  96. item.updated = jQuery(this).find('updated').eq(0).text();
  97. item.id = jQuery(this).find('id').eq(0).text();
  98. feed.items.push(item);
  99. });
  100. }
  101. };
  102. function JRss(xml) {
  103. this._parse(xml);
  104. };
  105. JRss.prototype = {
  106. _parse: function(xml) {
  107. if(jQuery('rss', xml).length == 0) this.version = '1.0';
  108. else this.version = jQuery('rss', xml).eq(0).attr('version');
  109. var channel = jQuery('channel', xml).eq(0);
  110. this.title = jQuery(channel).find('title:first').text();
  111. this.link = jQuery(channel).find('link:first').text();
  112. this.description = jQuery(channel).find('description:first').text();
  113. this.language = jQuery(channel).find('language:first').text();
  114. this.updated = jQuery(channel).find('lastBuildDate:first').text();
  115. this.items = new Array();
  116. var feed = this;
  117. jQuery('item', xml).each( function() {
  118. var item = new JFeedItem();
  119. item.title = jQuery(this).find('title').eq(0).text();
  120. item.link = jQuery(this).find('link').eq(0).text();
  121. item.description = jQuery(this).find('description').eq(0).text();
  122. item.updated = jQuery(this).find('pubDate').eq(0).text();
  123. item.id = jQuery(this).find('guid').eq(0).text();
  124. feed.items.push(item);
  125. });
  126. }
  127. };