Update to parse cleaner

This commit is contained in:
William Moore 2023-06-12 08:06:32 -05:00
parent 768d262f34
commit f9f962e54e

View File

@ -36,28 +36,41 @@ export class RSS extends LitElement {
};
try {
const allItems: RSSItem[] = [];
const response = await fetch(url, options);
const result = await response.text();
const rss = (new DOMParser()).parseFromString(result, 'text/xml');
console.log(rss);
const title = rss.getElementsByTagName('title')[0] ?? null;
const link = rss.getElementsByTagName('link')[0] ?? null;
this.rssTitle = title?.textContent ?? 'Undefined';
this.rssLink = link?.textContent ?? null;
const items = rss.getElementsByTagName('item');
const allItems: RSSItem[] = [];
for (let itemsI = 0; itemsI < items.length; itemsI++) {
const item = items.item(itemsI);
const itemTitle = item?.getElementsByTagName('title')[0] ?? null;
const itemLink = item?.getElementsByTagName('link')[0] ?? null;
allItems.push({
title: itemTitle?.textContent ?? 'Undefined',
link: itemLink?.textContent ?? null,
});
const channel = rss.getElementsByTagName('channel')[0] ?? null;
for (let channelI = 0; channelI < channel?.children?.length; channelI++) {
const channelChild = channel?.children?.item(channelI);
if (channelChild?.tagName === 'title') {
this.rssTitle = channelChild?.textContent ?? 'Undefined';
} else if (channelChild?.tagName === 'link') {
this.rssLink = channelChild?.textContent ?? null;
} else if (channelChild?.tagName === 'item') {
const items = channelChild?.children;
let itemTitle = '';
let itemLink = null;
for (let itemsI = 0; itemsI < items?.length; itemsI++) {
const item = items.item(itemsI);
if (item?.tagName === 'title') {
itemTitle = item?.textContent ?? 'Undefined';
} else if (item?.tagName === 'link') {
itemLink = item?.textContent ?? null;
}
}
allItems.push({
title: itemTitle,
link: itemLink,
});
}
}
this.rssItems = allItems;
this.requestUpdate();
} catch (error) {