Skip to content

Instantly share code, notes, and snippets.

@josher19
Created February 19, 2013 04:18
Show Gist options
  • Save josher19/4983079 to your computer and use it in GitHub Desktop.
Save josher19/4983079 to your computer and use it in GitHub Desktop.
RSS / Atom Feed Reader in Javascript
(function() {
var pre, div, txt, desc, here = 0;
function startup() {
pre = document.body.querySelector('pre');
txt = pre.innerText;
document.body.appendChild(div=document.createElement('div'));
div.innerHTML = txt;
div.style.display="none";
}
document.body.appendChild(desc=document.createElement('div'))
desc.style.border="1px solid green";
desc.style.padding = "1em";
desc.style.margin = "1em";
// desc.innerHTML = div.querySelectorAll('item')[here].querySelector('description').innerText
function getTitle(item) {
var title = item.querySelector('title').innerText;
return title.link(item.querySelector('link').nextSibling.textContent);
}
function uncdata(txt) { return txt.replace('<![CDATA[', '').replace(']]>', '').replace(']]&gt;', ''); }
function getTitleAtom(item) {
var title = uncdata(item.querySelector('title').innerText);
return title.link(item.querySelector('link').href);
}
function getTitleXml(item) {
var title = item.querySelector('title').textContent;
return title.link(item.querySelector('link').textContent);
}
function getPage(there) {
var items = div.querySelectorAll('item');
here=Math.max(there%items.length,0);
var item = items[here];
desc.innerHTML = '<h3>' + getTitle(item) + '</h3>' + item.querySelector('description').innerText
}
function getPageXml(there) {
var items = div.querySelectorAll('item');
here=Math.max(there%items.length,0);
var item = items[here];
desc.innerHTML = '<h3>' + getTitleXml(item) + '</h3>' + item.querySelector('description').textContent
}
function jqGetPage(there) {
here = there;
var item = jQuery(div).find('item').eq(there);
jQuery(desc).html( '<h3>' + getTitleXml(item[0]) + '</h3>' + item.find('description').next().text() );
}
function getPageAtom(there) {
var items = div.querySelectorAll('entry');
here=Math.max(there%items.length,0);
var item = items[here];
desc.innerHTML = '<h3>' + getTitleAtom(item) + '</h3>' + uncdata(item.querySelector('content').innerHTML)
}
function nextPage() { return getPage(++here) }
function prevPage() { return getPage(--here) }
desc.onclick = nextPage;
if (typeof jQuery !== "undefined") {
jQuery.get('/feed/', function(doc) {
div=doc;
getPage = jqGetPage;
getPage(here=1);
});
} else try {
startup();
getPage(here=0);
pre.style.display="none";
} catch(e) {
getPage = getPageAtom;
getPage(here=0);
pre.style.display="none";
}
// pre.style.display="none";
// http://blog.flurry.com/CMS/UI/Modules/BizBlogger/rss.aspx?tabid=96287&moduleid=107225&maxcount=25
var floater; document.body.appendChild(floater=document.createElement('div'));
floater.innerHTML = '<button id="prev">-</button>&nbsp;<button id="next">+</button>'
floater.style.position="fixed"; floater.style.left="45%"; floater.style.bottom="0";
floater.style.backgroundColor="rgba(125,125,125,0.25)";
document.getElementById('next').onclick = nextPage;
document.getElementById('prev').onclick = prevPage;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment