⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 feed-parser.js

📁 现在很火的邮件客户端软件thunderbird的源码
💻 JS
📖 第 1 页 / 共 2 页
字号:
      aFeed.onParseError(aFeed);      return parsedItems;    }    aFeed.title = aFeed.title || this.stripTags(getNodeValue(this.childrenByTagNameNS(channel, ATOM_03_NS, "title")[0]));    aFeed.description = getNodeValue(this.childrenByTagNameNS(channel, ATOM_03_NS, "tagline")[0]);    aFeed.link = this.findAtomLink("alternate",this.childrenByTagNameNS(channel, ATOM_03_NS, "link"));    if (!aFeed.parseItems)      return parsedItems;    aFeed.invalidateItems();    var items = this.childrenByTagNameNS(channel, ATOM_03_NS, "entry");    debug("Items to parse: " + items.length);      for (var i=0; i < items.length; i++)     {      var itemNode = items[i];      var item = new FeedItem();      item.feed = aFeed;      item.characterSet = "UTF-8";      var url;      url = this.findAtomLink("alternate",this.childrenByTagNameNS(itemNode, ATOM_03_NS, "link"));      item.url = url;      item.id = getNodeValue(this.childrenByTagNameNS(itemNode, ATOM_03_NS, "id")[0]);      item.description = getNodeValue(this.childrenByTagNameNS(itemNode, ATOM_03_NS, "summary")[0]);      item.title = getNodeValue(this.childrenByTagNameNS(itemNode, ATOM_03_NS, "title")[0])                                || (item.description ? item.description.substr(0, 150) : null)                                || item.title;      var authorEl = this.childrenByTagNameNS(itemNode, ATOM_03_NS, "author")[0]                     || this.childrenByTagNameNS(itemNode, ATOM_03_NS, "contributor")[0]                     || this.childrenByTagNameNS(channel, ATOM_03_NS, "author")[0];      var author = "";      if (authorEl)       {        var name = getNodeValue(this.childrenByTagNameNS(authorEl, ATOM_03_NS, "name")[0]);        var email = getNodeValue(this.childrenByTagNameNS(authorEl, ATOM_03_NS, "email")[0]);        if (name)          author = name + (email ? " <" + email + ">" : "");        else if (email)          author = email;      }            item.author = author || item.author || aFeed.title;      item.date = getNodeValue(this.childrenByTagNameNS(itemNode, ATOM_03_NS, "modified")[0]                               || this.childrenByTagNameNS(itemNode, ATOM_03_NS, "issued")[0]                               || this.childrenByTagNameNS(itemNode, ATOM_03_NS, "created")[0])                               || item.date;      // XXX We should get the xml:base attribute from the content tag as well      // and use it as the base HREF of the message.      // XXX Atom feeds can have multiple content elements; we should differentiate      // between them and pick the best one.      // Some Atom feeds wrap the content in a CTYPE declaration; others use      // a namespace to identify the tags as HTML; and a few are buggy and put      // HTML tags in without declaring their namespace so they look like Atom.      // We deal with the first two but not the third.            var content;      var contentNode = this.childrenByTagNameNS(itemNode, ATOM_03_NS, "content")[0];      if (contentNode)       {        content = "";        for (var j=0; j < contentNode.childNodes.length; j++)         {          var node = contentNode.childNodes.item(j);          if (node.nodeType == node.CDATA_SECTION_NODE)            content += node.data;          else            content += serializer.serializeToString(node);        }              if (contentNode.getAttribute('mode') == "escaped")         {          content = content.replace(/&lt;/g, "<");          content = content.replace(/&gt;/g, ">");          content = content.replace(/&amp;/g, "&");        }              if (content == "")          content = null;      }            item.content = content;      parsedItems[i] = item;    }    return parsedItems;  },  parseAsAtomIETF: function(aFeed, aDOM)  {        var parsedItems = new Array();    // Get the first channel (assuming there is only one per Atom File).    var channel = this.childrenByTagNameNS(aDOM,ATOM_IETF_NS,"feed")[0];    if (!channel)    {      aFeed.onParseError(aFeed);      return parsedItems;    }    aFeed.title = aFeed.title || this.stripTags(this.serializeTextConstruct(this.childrenByTagNameNS(channel,ATOM_IETF_NS,"title")[0]));    aFeed.description = this.serializeTextConstruct(this.childrenByTagNameNS(channel,ATOM_IETF_NS,"subtitle")[0]);    aFeed.link = this.findAtomLink("alternate", this.childrenByTagNameNS(channel,ATOM_IETF_NS,"link"));    if (!aFeed.parseItems)      return parsedItems;    aFeed.invalidateItems();    var items = this.childrenByTagNameNS(channel,ATOM_IETF_NS,"entry");    debug("Items to parse: " + items.length);    for (var i=0; i < items.length; i++)     {      var itemNode = items[i];      var item = new FeedItem();      item.feed = aFeed;      item.characterSet = "UTF-8";      item.isStoredWithId = true;      item.url = this.findAtomLink("alternate", this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "link")) || aFeed.link;      item.id = getNodeValue(this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "id")[0]);      item.description = this.serializeTextConstruct(this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "summary")[0]);      item.title = this.stripTags(this.serializeTextConstruct(this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "title")[0])                                  || (item.description ? item.description.substr(0, 150) : null)                                  || item.title);            // XXX Support multiple authors      var source = this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "source")[0];      var authorEl = this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "author")[0]                           || (source ? this.childrenByTagNameNS(source, ATOM_IETF_NS, "author")[0] : null)                           || this.childrenByTagNameNS(channel, ATOM_IETF_NS, "author")[0];      var author = "";      if (authorEl)       {        var name = getNodeValue(this.childrenByTagNameNS(authorEl, ATOM_IETF_NS, "name")[0]);        var email = getNodeValue(this.childrenByTagNameNS(authorEl, ATOM_IETF_NS, "email")[0]);        if (name)          author = name + (email ? " <" + email + ">" : "");        else if (email)          author = email;      }            item.author = author || item.author || aFeed.title;      item.date = getNodeValue(this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "updated")[0]                               || this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "published")[0])                               || item.date;      item.content = this.serializeTextConstruct(this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "content")[0]);      if(item.content)        item.xmlContentBase = this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "content")[0].baseURI;      else if(item.description)        item.xmlContentBase = this.childrenByTagNameNS(itemNode, ATOM_IETF_NS, "summary")[0].baseURI;      else        item.xmlContentBase = itemNode.baseURI;      parsedItems[i] = item;    }    return parsedItems;  },  serializeTextConstruct: function(textElement)  {    var content = "";    if (textElement)     {      var textType = textElement.getAttribute('type');      // Atom spec says consider it "text" if not present      if(!textType)        textType = "text";      // There could be some strange content type we don't handle      if((textType != "text") && (textType != "html") && (textType != "xhtml"))        return null;      for (var j=0; j < textElement.childNodes.length; j++)       {        var node = textElement.childNodes.item(j);        if (node.nodeType == node.CDATA_SECTION_NODE)          content += this.xmlEscape(node.data);        else          content += serializer.serializeToString(node);      }      if (textType == "html")         content = this.xmlUnescape(content);    }    // other parts of the code depend on this being null    // if there's no content    return content ? content : null;  },  // finds elements that are direct children of the first arg  childrenByTagNameNS: function(aElement, aNamespace, aTagName)  {    var matches = aElement.getElementsByTagNameNS(aNamespace, aTagName);    var matchingChildren = new Array();    for (var i = 0; i < matches.length; i++)     {      if(matches[i].parentNode == aElement)        matchingChildren.push(matches[i])    }    return matchingChildren;  },  findAtomLink: function(linkRel, linkElements)  {    // XXX Need to check for MIME type and hreflang    for ( var j=0 ; j<linkElements.length ; j++ ) {      var alink = linkElements[j];      if (alink &&           //if there's a link rel          ((alink.getAttribute('rel') && alink.getAttribute('rel') == linkRel) ||           //if there isn't, assume 'alternate'           (!alink.getAttribute('rel') && (linkRel=="alternate")))           && alink.getAttribute('href'))       {        // Atom links are interpreted relative to xml:base        var ioService = Components.classes["@mozilla.org/network/io-service;1"]                                   .getService(Components.interfaces.nsIIOService);        url = ioService.newURI(alink.baseURI, null, null);        return url.resolve(alink.getAttribute('href'));      }    }    return null;  },    stripTags: function(someHTML)   {    return someHTML.replace(/<[^>]+>/g,"");  },  xmlUnescape: function(s)  {    s = s.replace(/&lt;/g, "<");    s = s.replace(/&gt;/g, ">");    s = s.replace(/&amp;/g, "&");    return s;  },  xmlEscape: function(s)  {    s = s.replace(/&/g, "&amp;");    s = s.replace(/>/g, "&gt;");    s = s.replace(/</g, "&lt;");    return s;   }};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -