Adding a Wavelength Atom Feed Code Snippet

Below is an example of how to use the Google Feed Reader API to add the NASA Wavelength Atom feed to web-pages. Note, this is a “client-side” implementation (the blue arrows in the diagram above). By changing the link of the feedURL variable, you change they way you filter the Wavelength collection.

<!DOCTYPE html>
<html>
	<head>
	<title>My Web-page with  a Wavelength Feed!</title>
	<script src="https://www.google.com/jsapi" type="text/javascript"></script>
	<script type="text/javascript">
	var feedURL = "http://api.nasawavelength.org/search/cow-facets-atom?qq=&facetSort=1&educationalLevel=Informal+education&topicsSubjects=Earth+and+space+science%3AAstronomy%3AStars%2C+nebulae+and+galaxies&facetSort=1";
	var titlePrefix = "NASA-Wavelength Indexed Resources ";
	var feedLinkTitle = "Go to Wavelength for all items";

	//this function decodes escaped entities of URLs from the Atom feed
	function entityDecode(input){
		var e = document.createElement('div');
		e.innerHTML = input;
		return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
	}

	google.load("feeds", "1");
	function initialize() {
		if(feedURL != false){
			var feed = new google.feeds.Feed(feedURL);
			feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
			feed.load(function(result) {
				if (!result.error) {
					var OSnamespace = "http://a9.com/-/spec/opensearchrss/1.0/";
					var AtomNamespace = "http://www.w3.org/2005/Atom";

					var feedNode = result.xmlDocument.getElementsByTagNameNS(AtomNamespace, "feed");
					var osCountResult = result.xmlDocument.getElementsByTagNameNS("*", "totalResults");
					var countContainer = document.getElementById("osCount");
					countContainer.appendChild(document.createTextNode(titlePrefix + "(First " + result.feed.entries.length + " of "+ osCountResult[0].textContent + ")"));
					var linkContainer = document.getElementById("wavelengthLink");
					var linkDom = document.createElement("a");
					linkDom.appendChild(document.createTextNode(feedLinkTitle));
					linkDom.setAttribute("href", entityDecode(result.feed.link));
					linkDom.setAttribute("target","_blank");
					linkContainer.appendChild(linkDom );
					var container = document.getElementById("wavelengthFeed");
					var entryList = document.createElement("ul");
					container.appendChild(entryList);
					for (var i = 0; i < result.feed.entries.length; i++) {
						var entry = result.feed.entries[i];
						var entryItem = document.createElement("li");
						var alink = document.createElement("a");
						alink.appendChild(document.createTextNode(entry.title));
						alink.setAttribute("href", entry.link);
						entryItem.appendChild(alink);
						var sumSpan = document.createElement("span");
						sumSpan.setAttribute("style", "margin-left:5px;");
						sumSpan.appendChild(document.createTextNode(entityDecode(entry.content)));
						entryItem.appendChild(sumSpan);
						entryList.appendChild(entryItem);
					}
				}
			});
		}
	}
	
	google.setOnLoadCallback(initialize);

</script>
</head>

<body>
	<h1>Feed Demo</h1>
	<h4 id="osCount"></h4>
	<p id="wavelengthLink"></p>
	<div id="wavelengthFeed"></div>
</body>    
</html>