Skip to content

Instantly share code, notes, and snippets.

Created August 31, 2011 12:14
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Feed Autodiscovery using Javascript, the Google feed API and YQL
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Feed Autodiscovery</title>
<script src="" type="text/javascript"></script>
<script type="text/javascript">
google.load("feeds", "1");
google.load("jquery", "1.6.2");
function setup() {
getUrlVars: function () {
var vars = [],
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
return vars;
getUrlVar: function (name) {
return $.getUrlVars()[name];
function OnLoad() {
if (!($.getUrlVar('url'))) $('#url').val('');
else $('#url').val(unescape($.getUrlVar('url')));
function yqlAutodisc(){
var yqurl=''+escape($('#url').val())+'&format=json'
$.getJSON(yqurl, function(response){
// response is a variable containing the JSON object, if it fetched it successfully
if (!(response.query.count))
$('#ycontent').html('<em>No autodiscoverable feeds...</em>')
else {
$.each(, function(i,item){
$('#ycontent').append('<div><em>'+item.title+'</em>: <a href="'+item.href+'">'+item.href+'</a></div>')
function lookupFeed() {
// Go find it! Call lookupDone when the search is complete.
var url = $('#url').val()
google.feeds.lookupFeed(url, lookupDone);
function lookupDone(result) {
// Make sure we didn't get an error.
if (!result.error && result.url != null) {
var url = result.url;
// Print the feed found to the page. Note that we only ever get one result
// back from a lookupFeed.
$('#content').html('<a href="' + url + '">' + url + '</a>');
} else $('#content').html("<em>No feed found...</em>")
function feedLoaded(result) {
if (!result.error) {
// Loop through the feeds, putting the titles onto the page.
// Check out the result object for a list of properties returned in each entry.
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
$('#histcontent').append('<div>' + i + ': <a href="''">' + entry.title + '</a></div>');
} else $('#histcontent').html('<em>Something appears to be wrong:-(</em>')
function lookupFeedHistory() {
var url = $('#furl').val()
var feed = new google.feeds.Feed(url);
feed.includeHistoricalEntries(); // tell the API we want to have old entries too
feed.setNumEntries($('#furlmax').val()); // should really noddy check this first for an appropriate value
// Calling load sends the request off. It requires a callback function.
<body style="font-family: Arial;border: 0 none;">
<form action="javascript:lookupFeed()"><span>Try your own URL:</span> <input type="text" size="60" id="url"/> <input id="newgo" type="submit" value="Go"/></form>
<div>Top feed for this page (via Google feed api): <span id="content"></span><br/><br/>
Feeds detected via YQL: <span id="ycontent"></span></div>
<form action="javascript:lookupFeedHistory()"><span>Try your own feed URL:</span> <input type="text" size="60" id="furl"/> Items: <input type="text" size="5" id="furlmax" value='10'/> (max 250) <input id="histgo" type="submit" value="Go"/></form>
<div>Historical entries for this feed: <span id="histcontent"></span></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment