Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created October 16, 2009 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboesch/211822 to your computer and use it in GitHub Desktop.
Save jboesch/211822 to your computer and use it in GitHub Desktop.
/* This is a template command. */
CmdUtils.CreateCommand({
names: ["rss"],
icon: "http://www.feedicons.com/favicon.ico",
description: "A nice and quick way to browse RSS feeds.",
help: "Type 'rss' then the url of the site. example: rss www.boedesign.com. If there are multiple feeds you know of try doing rss www.boedesign.com(2) to get another feed.",
author: {name: "Jordan Boesch", email: "jboesch26@gmail.com"},
license: "GPL",
homepage: "http://boedesign.com.com/",
arguments: [{role: 'object', nountype: noun_arb_text}],
_html: '',
_valid_rss: ['.xml', '.rss'],
preview: function preview(pblock, args) {
var txt = args.object.html;
pblock.innerHTML = "Fetching feed for <b>" + txt + "</b>...";
main_this = this;
main_this._html = '';
var feed_num = 0;
var bypass_rss_check = false; // if they type in a url with .xml or .rss in it, we will just use that
var found_feed = false;
if(this._isValidRssExt(txt)) bypass_rss_check = true;
if(txt.indexOf('http://') == -1) txt = 'http://' + txt;
if(txt.indexOf('@') != -1) {
txt_feed_num = this._filterTxt(txt);
txt = txt_feed_num[0];
feed_num = txt_feed_num[1];
}
// get the entire page to try to figure out where the RSS link is...
jQuery.get(txt, {}, function(d){
var rss_links = [];
var rss_link = '';
if(bypass_rss_check){
rss_link = txt;
}
else {
// recursive parent
var items = jQuery(d).find('a:eq(0)').parents().each(function(){
//displayMessage(find_str);
var links = jQuery(this).find('link');
jQuery(links).each(function(i){
if(jQuery(this).attr('type') == 'application/rss+xml'){
rss_links.push(jQuery(this).attr('href'));
}
});
});
var rss_feed_index = (feed_num > 0) ? (feed_num - 1) : 0;
if(rss_links.length > 0) rss_link = rss_links[rss_feed_index];
if(!rss_link && !found_feed){
pblock.innerHTML = 'We could not find an RSS link on that site! The RSS link must appear in the <head> tag of the site.';
}
}
// now get the appropriate feed from rss_link
jQuery.get(rss_link, {}, function(data){
var obj = data;
var items = jQuery(data).find('item');
jQuery(items).each(function(i){
// required elements
var title = jQuery(this).find('title').text();
var link = jQuery(this).find('link').text();
var desc = jQuery(this).find('description').text();
// optional
var pub_date = jQuery(this).find('pubDate').text();
var cat_arr = [];
var cat = '';
jQuery(this).find('category').each(function(){ cat_arr.push(jQuery(this).text()); });
if(cat_arr.length > 0){
cat = cat_arr.join(', ');
}
// output
main_this._html += '<h3 style="color:red"><a href="' + link + '">' + title + '</a></h3>';
main_this._html += '<p>' + desc + '</p>';
if(cat) main_this._html += '<strong>Category:</strong> ' + cat + '<br/>';
if(pub_date) main_this._html += '<strong>Publication Date:</strong> ' + pub_date + '<br/>';
});
found_feed = true;
var template = "Feed: " + rss_link + "${posts}";
pblock.innerHTML = CmdUtils.renderTemplate(template, {"posts": main_this._html});
}, 'xml');
});
},
 
execute: function execute(args) {
displayMessage("You selected: " + args.object.text, this);
},
_filterTxt: function(txt){
var num = txt.split('@')[1];
var text = txt.split('@')[0];
return [text, num];
},
_isValidRssExt: function(txt){
jQuery(this._valid_rss).each(function(i, val){
if(txt.indexOf(val) != -1) return true;
});
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment