Skip to content

Instantly share code, notes, and snippets.

@robertito13 robertito13/rss.js
Created Aug 17, 2018

Embed
What would you like to do?
document.addEventListener( 'DOMContentLoaded', function() {
const websiteUrl = 'http://www.anred.org/?feed=rss2';
const entityPattern = /\[(&(?:\#(?:(?<dec>[0-9]+)|[Xx](?<hex>[0-9A-Fa-f]+))|(?<named>[A-Za-z0-9]+));)\]/
const feeds = [
'http://www.anred.org/?feed=rss2'
];
feeds.forEach( function( websiteUrl ) {
fetch( websiteUrl ).then( ( res ) => {
res.text().then( ( htmlTxt ) => {
var domParser = new DOMParser();
let doc = domParser.parseFromString( htmlTxt, 'text/html' );
let items = doc.querySelectorAll( 'item' );
for ( let i = 0; 2 > i; i++ ) {
let title = items[i].querySelector( 'title' ).textContent;
let excerpt = items[i].querySelector( 'description' );
excerpt = excerpt.innerHTML;
excerpt = excerpt.replace( '<!--[CDATA[', '' );
excerpt = excerpt.replace( ']]-->', '' );
excerpt = excerpt.replace( entityPattern, '$1' );
excerpt = excerpt.replace( ' &#8230;', '&#8230;' );
excerpt = excerpt.replace( '.&#8230;', '&#8230;' );
let elemImage = items[i].querySelector( 'img' );
elemImage.removeAttribute( 'class' );
elemImage.removeAttribute( 'width' );
elemImage.removeAttribute( 'height' );
elemImage.style.display = 'block';
let article = document.createElement( 'article' );
article.setAttribute( 'class', 'left-side' );
article.style.display = 'block';
article.appendChild( elemImage );
let elemTitle = document.createElement( 'a' );
elemTitle.setAttribute( 'class', 'title' );
elemTitle.textContent = title;
article.appendChild( elemTitle );
let elemExcerpt = document.createElement( 'div' );
elemExcerpt.setAttribute( 'class', 'excerpt' );
elemExcerpt.innerHTML = excerpt;
article.appendChild( elemExcerpt );
let container = document.createElement( 'div' );
container.setAttribute( 'class', 'full-width' );
container.appendChild( article );
let output = document.querySelector( '.output' );
output.appendChild( container );
};
});
}).catch( () => console.error( 'Error in fetching the website' ) );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.