RSS 阅读器:获取、去重与安全呈现
从本地feed实际读取条目,区分旧产品清单与当前浏览器能力。
学完这一章,你能够
- 从本地feed实际读取条目,区分旧产品清单与当前浏览器能力。
- 核对频道与item不同的必需条件
- 让每个图像、附件、日期和实例对应真实输入
不同实现
阅读器可以是网络服务、桌面、移动应用或扩展,负责获取、去重、排序和展示。将feed URL复制到阅读器是常见订阅方式,但不同版本和扩展模块支持仍需测试。
NewsGator Online、RssReader、FeedDemon和blogbot是旧教程列举的产品,保留其在线、Windows或Outlook/IE插件等历史定位,不保证当前下载、免费、同步能力及兼容性仍相同。
已有阅读器时,可以从RSS或XML订阅图标取得真正feed的URL,再在阅读器中添加;不要把普通网页地址当作订阅数据。
从RSS图标取得订阅地址后,复制到实际阅读器;图标本身不是阅读器实现。
历史XML图标也可表示订阅入口,当前浏览器是否内置阅读需另核对。
浏览器能力已变化
Firefox内建feed预览及Live Bookmarks自2018年12月的64版移除,不能继续提示所有当前Firefox用户点击地址栏内置RSS图标。可按实际环境使用相应阅读器或扩展,图标链接只负责引导订阅。
本站reader.html从固定同源feed获取XML,使用DOM方法及textContent构建列表,只允许HTTP/HTTPS链接。读取不可信feed时还要限制大小、外部实体及资源访问,不能把所有订阅内容当可信网页执行。
真实观察范围
工程实际核对两个条目的标题、GUID、附件及空/不合法链接处理,并验证文本中的HTML字样不会执行。命令行解析、浏览器显示和真实媒体播放是不同证据,不把其中一项自动当作全部通过。
历史阅读器完整保留
| 名称 | 旧资料定位 | 现在的说明 |
| NewsGator Online | 在线及相关同步功能 | 历史清单,不保证当前服务 |
| RssReader | Windows阅读器及多格式支持 | 历史版本范围需另核对 |
| FeedDemon | Windows阅读器 | 不保证旧下载或免费状态 |
| blogbot | Outlook/IE插件 | 历史宿主,不当作当前推荐 |
| Firefox内建feed支持 | 曾有预览及Live Bookmarks | 64版于2018年12月移除 |
本站真实阅读分支
| 输入 | 结果 | 边界 |
| feed.xml | 两个条目,PNG真实加载,输入框出现 | 同源固定feed |
| 十三个实例 | 分别实际读取 | 每例只验证明确目标 |
| 重复GUID | 显示一条并记录一次去重 | 稳定标识 |
| javascript链接 | 不生成危险链接 | 协议白名单 |
| HTML样式描述 | textContent显示为文字 | 不执行onerror等内容 |
| 评论/媒体 | 实际本地目标可取得 | 不代表所有阅读器实现相同 |
本章独立核心源码与验证步骤
展开查看收起
html
<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><title>RSS 阅读实验 | 蓝色天空网</title><link rel="alternate" type="application/rss+xml" title="蓝色天空网学习更新" href="/feed.xml"></head><body><h1>蓝色天空网 mbsky.com</h1><p>订阅内容以文本安全显示;请使用本地学习feed。</p><a href="/feed">RSS订阅</a><img id="channel-image" hidden alt=""><h2 id="channel"></h2><div id="items"></div><div id="search"></div><p id="status" role="status"></p><script>
const files=["feed.xml", "unsafe.xml", "duplicates.xml", "examples/channel-required.xml", "examples/channel-category.xml", "examples/channel-generator.xml", "examples/channel-image.xml", "examples/channel-language.xml", "examples/channel-textInput.xml", "examples/channel-skipDays.xml", "examples/item-common.xml", "examples/item-comments.xml", "examples/item-enclosure.xml", "examples/item-guid.xml", "examples/item-pubDate.xml", "examples/item-source.xml"];
async function load(){try{const selected=new URLSearchParams(location.search).get('feed')||'feed.xml';if(!files.includes(selected))throw Error('不允许的本地feed');const response=await fetch('/'+selected,{headers:{Accept:'application/rss+xml'}});if(!response.ok)throw Error('HTTP '+response.status);const text=await response.text();if(text.length>1000000||/<!DOCTYPE/i.test(text))throw Error('输入超限或含DTD');const doc=new DOMParser().parseFromString(text,'application/xml');if(doc.querySelector('parsererror'))throw Error('XML解析失败');const channel=doc.querySelector('rss > channel');if(!channel)throw Error('不是RSS2频道');document.querySelector('#channel').textContent=channel.querySelector(':scope > title')?.textContent||'';const feedImage=channel.querySelector(':scope > image');if(feedImage){const imageURL=new URL(feedImage.querySelector('url').textContent);if(imageURL.origin===location.origin){const image=document.querySelector('#channel-image');image.alt=feedImage.querySelector('title').textContent;await new Promise((resolve,reject)=>{image.onload=resolve;image.onerror=()=>reject(Error('频道图像加载失败'));image.src=imageURL.href});image.hidden=false}}let unsafeLinks=0,duplicates=0;const seen=new Set();for(const item of channel.querySelectorAll(':scope > item')){const guid=item.querySelector(':scope > guid')?.textContent;if(guid&&seen.has(guid)){duplicates++;continue}if(guid)seen.add(guid);const section=document.createElement('section');const title=item.querySelector(':scope > title')?.textContent||'未命名条目';const href=item.querySelector(':scope > link')?.textContent;let node=document.createElement('span');if(href){const url=new URL(href,location.href);if(['http:','https:'].includes(url.protocol)){node=document.createElement('a');node.href=url.href;node.rel='noopener noreferrer'}else unsafeLinks++}node.textContent=title;section.append(node);const p=document.createElement('p');p.textContent=item.querySelector(':scope > description')?.textContent||'';section.append(p);for(const [field,label] of [['comments','查看讨论'],['enclosure','下载媒体']]){const element=item.querySelector(':scope > '+field);const href=field==='enclosure'?element?.getAttribute('url'):element?.textContent;if(href){const target=new URL(href,location.href);if(['http:','https:'].includes(target.protocol)){const a=document.createElement('a');a.href=target.href;a.textContent=label;a.rel='noopener noreferrer';if(field==='enclosure')a.download='';section.append(a)}}}document.querySelector('#items').append(section)}const ti=channel.querySelector(':scope > textInput');if(ti){const action=new URL(ti.querySelector('link').textContent);if(action.origin===location.origin){const form=document.createElement('form');form.method='get';form.action=action.href;const input=document.createElement('input');input.name=ti.querySelector('name').textContent;input.setAttribute('aria-label',ti.querySelector('description').textContent);const button=document.createElement('button');button.textContent=ti.querySelector('title').textContent;form.append(input,button);document.querySelector('#search').append(form)}}window.__rssResult={title:document.querySelector('#channel').textContent,items:document.querySelectorAll('#items section').length,unsafeLinks,duplicates,imageWidth:document.querySelector('#channel-image').naturalWidth,textInput:!!document.querySelector('#search form'),descriptions:[...document.querySelectorAll('#items p')].map(p=>p.textContent),inlineHandlers:document.querySelectorAll('#items [onerror]').length,passed:true};document.querySelector('#status').textContent='读取完成';}catch(error){window.__rssResult={passed:false,error:error.message};document.querySelector('#status').textContent=error.message}}
load();</script></body></html>
尝试一下 ↗ 在独立页面修改并运行
- 本章列明的RSS条件与实际解析/阅读结果一致
- 不把替代图像、生成WAV或教学检查器冒充原站采集或全规范验收
本章附件与实例包
课程关联工程包(登记关联9章) · 含当前章节或实例 · mbskycom202611a528.zip
常见错误与边界检查
- 把item三项一律当成必填。
- 把ttl分钟与HTTP缓存秒混同。
- 把历史阅读器、目录或原图替代说成当前已验证。
技术规范与官方资料
练习与参考解答
查看参考解答
先检查XML与RSS2条件,再比较具体资源字节和实际阅读结果;未知扩展及外部服务不自动记为通过。