RSSFinder/rssfinder/finder.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

(function() {
var availableLinks = [];
var links = document.getElementsByTagName('link');
var siteTitle = 'No title was found';
if (document.getElementsByTagName('title').lenght > 0) {
siteTitle = document.getElementsByTagName('title')[0].innerHTML
}
for (link of links) {
if ( (link.getAttribute('rel') == 'alternate')
&& ( (link.getAttribute('type') == 'application/rss+xml')
|| (link.getAttribute('type') == 'application/atom+xml')
|| (link.getAttribute('type') == 'application/stream+json')
|| (link.getAttribute('type') == 'application/rdf+xml')
)
) {
var linkTitle;
var linkUrl;
if (link.hasAttribute('title')) {
linkTitle = link.getAttribute('title');
} else {
linkTitle = siteTitle;
}
if (link.hasAttribute('href') == true) {
if (link.getAttribute('href').startsWith('/')) {
linkUrl = window.location.protocol + '//' + window.location.hostname + link.getAttribute('href');
} else if (!link.getAttribute('href').includes('/')) {
linkUrl = window.location.protocol + '//' + window.location.hostname + '/' + link.getAttribute('href');
} else {
linkUrl = link.getAttribute('href');
}
availableLinks.push({
title: linkTitle,
url: linkUrl
});
}
}
}
if (availableLinks.length == 0) {
availableLinks.push({
title: 'No RSS feed was found',
url: 'No URL is provided'
});
}
var out = {
links: availableLinks
};
return out;
})()