mulemister / r/photography Flair Linkr

// ==UserScript==
// @name       r/photography Flair Linkr
// @namespace  http://www.flickr.com/photos/antrover
// @icon http://www.redditstatic.com/favicon.ico
// @version    0.2
// @description  Automatically link'ify /r/photography users displaying flair
// @match      *://www.reddit.com/r/photography*
// @grant  GM_getValue
// @grant  GM_setValue
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js
// @run-at document-end
// @author cakes_and_pies
// ==/UserScript==

// For debug purposes...
  // var js = document.createElement("script");
  // js.type = "text/javascript";
  // js.src = 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js';

jQuery.noConflict();
var underscore = _.noConflict();

function FlairEnhancer(args){
  this.target_class = args.target_class;
  this.base_url = args.base_url;  

  this.populate = function(anchor_tag, link, element){
    anchor_tag.attr({'href': link, 'target': '_blank', 'title': link});
    anchor_tag.text(link);
    jQuery(element).text('');
    jQuery(element).append(anchor_tag);    
  };
}

// currently only works for flickr, 500px, instagram and facebook...
FlairEnhancer.prototype.enhance = function(){
  var self = this;
  var _class = '.flair.flair-'+self.target_class;

  jQuery.each(jQuery(_class), function(it, item){
    var _flair_text       = jQuery(item).text(),
        _anchor_tag       = jQuery('<a />'),
        _attr_split_title = underscore.compact(jQuery(item).attr('title').split('/'));

    var _name = _attr_split_title.length > 1 ? _attr_split_title[_attr_split_title.length - 1] : _attr_split_title[0];
    var _link = self.base_url + _name;
    self.populate(_anchor_tag, _link, item);
  });
};

// Enhances custom domains.
// TODO: Cleanup some duplication between this function and enhance()
FlairEnhancer.prototype.enhanceCustomURL = function(){
  var self = this;
  var _class = '.flair.flair-'+this.target_class;

  jQuery.each(jQuery(_class), function(it, item){
    var _flair_text       = jQuery(item).text(),
        _anchor_tag       = jQuery('<a />'),
        _title             = jQuery(item).attr('title'),
        _link;

    var _includes_http = '(https?).*';
    var _includes_http_reg = new RegExp(_includes_http);

    if(_title.match(_includes_http_reg)){
      _link = _title;
    }else{
      _link = 'http://' + _title;
    }
    self.populate(_anchor_tag, _link, item);
  });
};


var flickr = new FlairEnhancer({
  target_class: 'flickr',
  base_url: 'http://flic.kr/'
});

var five_hundred_pixels = new FlairEnhancer({
  target_class: '500px',
  base_url: 'http://www.500px.com/'
});

var instagram = new FlairEnhancer({
  target_class: 'instagram',
  base_url: 'http://www.instagram.com/'
});

var facebook = new FlairEnhancer({
  target_class: 'facebook',
  base_url: 'https://www.facebook.com/'
});

var custom_domain = new FlairEnhancer({
  target_class: 'website',
  base_url: undefined
});

flickr.enhance();
five_hundred_pixels.enhance();
instagram.enhance();
facebook.enhance();
custom_domain.enhanceCustomURL();