public
Last active

  • Download Gist
x.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/* 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;
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.